1、鸢尾花数据集概述
机器学习包sklearn中集成了各种各样的数据集,其中就包括鸢尾花数据集(Iris)是最简单的分类任务数据集之一。
鸢尾花数据集共有3个分类类别,分别是山鸢尾(Iris-setosa)、变色鸢尾(Iris-versicolor)和维吉尼亚鸢尾(Iris-virginica)
该数据集共有150个样本,5个变量(4个特征变量,1个类别变量)。iris是鸢尾植物,4个特征分别对应萼片和花瓣的长和宽。如下表:
列名 | 说明 | 类型 |
---|---|---|
SepalLength | 花萼长度 | float |
SepalWidth | 花萼宽度 | float |
PetalLength | 花瓣长度 | float |
PetalWidth | 花瓣宽度 | float |
Class | 类别变量:0表示山鸢尾、 1表示变色鸢尾、 2表示维吉尼亚鸢尾 | float |
2、鸢尾花数据获取
from sklearn import datasets # 导入sklearn中集成的数据集
import pandas as pd # 导入pandas
iris = datasets.load_iris()
print("鸢尾花特征:\n", iris.data[:5]) # print前5个特征
print("鸢尾花特征的维度:\n", iris.data.shape)
print("鸢尾花标签:\n", iris.target)
print("鸢尾花标签的维度:\n", iris.target.shape)
输出如下所示:
鸢尾花特征:
[[5.1 3.5 1.4 0.2]
[4.9 3. 1.4 0.2]
[4.7 3.2 1.3 0.2]
[4.6 3.1 1.5 0.2]
[5. 3.6 1.4 0.2]]
鸢尾花特征的维度:
(150, 4)
鸢尾花标签:
[0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 2 2 2 2 2 2 2 2 2 2 2
2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2
2 2]
鸢尾花标签的维度:
(150,)
从输出结果可以看到,类别标签共分为三类,前面50个类标位0,中间50个类标位1,后面50个类别为2。分别代表为山鸢尾、杂色鸢尾、维吉尼亚鸢尾。
3、 散点图绘制
用scatter()函数绘制散点图。
from sklearn import datasets # 导入sklearn中集成的数据集
import pandas as pd # 导入pandas
import matplotlib.pyplot as plt
iris = datasets.load_iris()
print("鸢尾花特征:\n", iris.data[:5]) # print前5个特征
print("鸢尾花特征的维度:\n", iris.data.shape)
print("鸢尾花标签:\n", iris.target)
print("鸢尾花标签的维度:\n", iris.target.shape)
cols_list = ['SepalLength', 'SepalWidth', 'PetalLength', 'PetalWidth']
df_iris = pd.DataFrame(iris.data, columns=cols_list)
df_iris['Class'] = iris.target
plt.figure()
plt.scatter(df_iris.loc[:50, 'SepalLength'], df_iris.loc[:50, 'SepalWidth'], color='red', marker='o', label='setosa')
plt.scatter(df_iris.loc[50:100, 'SepalLength'], df_iris.loc[50:100, 'SepalWidth'], color='blue', marker='x',
label='versicolor')
plt.scatter(df_iris.loc[100:, 'SepalLength'], df_iris.loc[100:, 'SepalWidth'], color='green', marker='+', label='Virginica')
plt.show()
绘制散点图如图所示:
如需要整理好的详细的鸢尾花数据集、数据说明、可直接建模的数据等资料,可访问 Python编程网 获取