Scikit-learn iris 2019-12-18

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()
Figure 3. Scatter of sepal size and type of iris.png

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()
Figure 2. Scatter of petal size and type of iris.png

(本学习过程根据知乎网友的分享学习:
https://zhuanlan.zhihu.com/p/31785188,
其内容中的代码或注释有两处小错误,对于看懂的读者不会受到影响)

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。