首页 > 分享 > 鸢尾花数据集 — scikit

鸢尾花数据集 — scikit

鸢尾花数据集#

该数据集包含 3 种不同鸢尾花(山鸢尾、变色鸢尾和维吉尼亚鸢尾)的花瓣和萼片长度,存储在一个 150x4 的 numpy.ndarray 中。

行代表样本,列代表:萼片长度、萼片宽度、花瓣长度和花瓣宽度。

下图使用了前两个特征。有关此数据集的更多信息,请参见此处。

# Code source: Gaël Varoquaux # Modified for documentation by Jaques Grobler # License: BSD 3 clause

加载鸢尾花数据集#

鸢尾花数据集散点图#

import matplotlib.pyplot as plt _, ax = plt.subplots() scatter = ax.scatter(iris.data[:, 0], iris.data[:, 1], c=iris.target) ax.set(xlabel=iris.feature_names[0], ylabel=iris.feature_names[1]) _ = ax.legend( scatter.legend_elements()[0], iris.target_names, loc="lower right", title="Classes" )

散点图中的每个点代表数据集中 150 朵鸢尾花中的一朵,颜色表示其各自的类型(山鸢尾、变色鸢尾和维吉尼亚鸢尾)。您已经可以看到关于山鸢尾类型的一种模式,它可以根据其短而宽的萼片轻松识别。仅考虑这两个维度,萼片宽度和长度,变色鸢尾和维吉尼亚鸢尾类型之间仍然存在重叠。

绘制 PCA 表示#

让我们对鸢尾花数据集应用主成分分析 (PCA),然后绘制前三个 PCA 维度上的鸢尾花。这将使我们能够更好地区分这三种类型!

# unused but required import for doing 3d projections with matplotlib < 3.2 import mpl_toolkits.mplot3d # noqa: F401 from sklearn.decomposition import PCA fig = plt.figure(1, figsize=(8, 6)) ax = fig.add_subplot(111, projection="3d", elev=-150, azim=110) X_reduced = PCA(n_components=3).fit_transform(iris.data) ax.scatter( X_reduced[:, 0], X_reduced[:, 1], X_reduced[:, 2], c=iris.target, s=40, ) ax.set_title("First three PCA dimensions") ax.set_xlabel("1st Eigenvector") ax.xaxis.set_ticklabels([]) ax.set_ylabel("2nd Eigenvector") ax.yaxis.set_ticklabels([]) ax.set_zlabel("3rd Eigenvector") ax.zaxis.set_ticklabels([]) plt.show()

PCA 将创建 3 个新特征,它们是 4 个原始特征的线性组合。此外,此变换最大化了方差。通过这种变换,我们看到我们可以仅使用第一个特征(即第一个特征值)来识别每个物种。

脚本总运行时间:(0 分钟 0.227 秒)

相关示例

由 Sphinx-Gallery 生成的图库

相关知识

鸢尾花数据集下载
python鸢尾花数据集的分类问题 -- 逻辑回归问题研究
分析鸢尾花数据集
实验一:鸢尾花数据集分类
《机器学习》分析鸢尾花数据集
机器学习数据集之鸢尾花
MATLAB鸢尾花数据集(iris.txt)
KNN算法实现鸢尾花数据集分类
深度学习入门之鸢尾花数据集读入
鸢尾花数据集可视化分析

网址: 鸢尾花数据集 — scikit https://m.huajiangbk.com/newsview546207.html

所属分类:花卉
上一篇: [Python] 机器学习
下一篇: 大数据填报志愿乱象:花数百元看不