1. Aim
Today, I will learn to practice to plot some data in scikit-learn by iris data.
2. Iris
Iris was an inner data in scikit-learn.
2.1 import iris data
from sklearn import datasets
iris = datasets.load_iris()
iris.data
iris.target
iris.target_names
2.2 Plot a scatter to show the type of iris, and x axis stands for sepal length, y stands for sepal width.
import matplotlib.pyplot as plt
from sklearn import datasets
iris = datasets.load_iris()
x = iris.data[:,0]
y = iris.data[:,1]
species = iris.target
x_min, x_max = x.min() - 0.5, x.max()+0.5
y_min, y_max = y.min() - 0.5, y.max()+0.5
# scatterplot
plt.figure
plt.title('Iris Dataset - Classification By Sepal Sizes')
plt.scatter(x, y, c = species)
plt.xlabel('Sepal length')
plt.ylabel('Sepal width')
plt.xlim(x_min, x_max)
plt.ylim(y_min, y_max)
plt.xticks(())
plt.yticks(())
plt.show()
2.3 Plot a scatter to show the type of iris, and x axis stands for petal length, y stands for petal width.
import matplotlib.pyplot
import matplotlib.patches as mpatches
from sklearn import datasets
iris = datasets.load_iris()
x = iris.data[:,2]
y = iris.data[:,3]
species = iris.target
x_min, x_max = x.min() - 0.5, x.max()+0.5
y_min, y_max = y.min() - 0.5, y.max()+0.5
# scatterplot
plt.figure
plt.title('Iris Dataset - Classification By Petal Sizes')
plt.scatter(x, y, c = species)
plt.xlabel('Petal length')
plt.ylabel('Petal width')
plt.xlim(x_min, x_max)
plt.ylim(y_min, y_max)
plt.xticks(())
plt.yticks(())
plt.show()
(本學(xué)習(xí)過程根據(jù)知乎網(wǎng)友的分享學(xué)習(xí):
https://zhuanlan.zhihu.com/p/31785188,
其內(nèi)容中的代碼或注釋有兩處小錯(cuò)誤再来,對于看懂的讀者不會(huì)受到影響)