.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "auto_examples/decomposition/plot_pca_iris.py" .. LINE NUMBERS ARE GIVEN BELOW. .. only:: html .. note:: :class: sphx-glr-download-link-note :ref:`Go to the end ` to download the full example code. or to run this example in your browser via Binder .. rst-class:: sphx-glr-example-title .. _sphx_glr_auto_examples_decomposition_plot_pca_iris.py: ========================================================= 使用鸢尾花数据集的PCA示例 ========================================================= 主成分分析应用于鸢尾花数据集。 有关此数据集的更多信息,请参见 `这里 `_ 。 .. GENERATED FROM PYTHON SOURCE LINES 12-60 .. image-sg:: /auto_examples/decomposition/images/sphx_glr_plot_pca_iris_001.png :alt: plot pca iris :srcset: /auto_examples/decomposition/images/sphx_glr_plot_pca_iris_001.png :class: sphx-glr-single-img .. code-block:: Python # 代码来源:Gaël Varoquaux # SPDX许可证标识符:BSD-3-Clause import matplotlib.pyplot as plt # 未使用但需要导入以使用低于3.2版本的matplotlib进行3D投影 import mpl_toolkits.mplot3d # noqa: F401 import numpy as np from sklearn import datasets, decomposition np.random.seed(5) iris = datasets.load_iris() X = iris.data y = iris.target fig = plt.figure(1, figsize=(4, 3)) plt.clf() ax = fig.add_subplot(111, projection="3d", elev=48, azim=134) ax.set_position([0, 0, 0.95, 1]) plt.cla() pca = decomposition.PCA(n_components=3) pca.fit(X) X = pca.transform(X) for name, label in [("Setosa", 0), ("Versicolour", 1), ("Virginica", 2)]: ax.text3D( X[y == label, 0].mean(), X[y == label, 1].mean() + 1.5, X[y == label, 2].mean(), name, horizontalalignment="center", bbox=dict(alpha=0.5, edgecolor="w", facecolor="w"), ) # 重新排序标签以使颜色与聚类结果匹配 y = np.choose(y, [1, 2, 0]).astype(float) ax.scatter(X[:, 0], X[:, 1], X[:, 2], c=y, cmap=plt.cm.nipy_spectral, edgecolor="k") ax.xaxis.set_ticklabels([]) ax.yaxis.set_ticklabels([]) ax.zaxis.set_ticklabels([]) plt.show() .. rst-class:: sphx-glr-timing **Total running time of the script:** (0 minutes 0.038 seconds) .. _sphx_glr_download_auto_examples_decomposition_plot_pca_iris.py: .. only:: html .. container:: sphx-glr-footer sphx-glr-footer-example .. container:: binder-badge .. image:: images/binder_badge_logo.svg :target: https://mybinder.org/v2/gh/scikit-learn/scikit-learn/main?urlpath=lab/tree/notebooks/auto_examples/decomposition/plot_pca_iris.ipynb :alt: Launch binder :width: 150 px .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: plot_pca_iris.ipynb ` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: plot_pca_iris.py ` .. container:: sphx-glr-download sphx-glr-download-zip :download:`Download zipped: plot_pca_iris.zip ` .. include:: plot_pca_iris.recommendations .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_