.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "auto_examples/decomposition/plot_incremental_pca.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_incremental_pca.py: =============== 增量PCA =============== 增量主成分分析(IPCA)通常在数据集过大无法全部加载到内存时,作为主成分分析(PCA)的替代方法使用。IPCA使用与输入数据样本数量无关的内存量,为输入数据构建低秩近似。它仍然依赖于输入数据的特征,但通过改变批量大小可以控制内存使用。 这个例子作为一个视觉检查,展示了IPCA能够找到与PCA相似的数据投影(符号翻转除外),同时每次只处理少量样本。这可以被认为是一个“玩具示例”,因为IPCA是为那些无法全部加载到主内存的大型数据集设计的,需要增量方法。 .. GENERATED FROM PYTHON SOURCE LINES 12-55 .. rst-class:: sphx-glr-horizontal * .. image-sg:: /auto_examples/decomposition/images/sphx_glr_plot_incremental_pca_001.png :alt: Incremental PCA of iris dataset Mean absolute unsigned error 0.002201 :srcset: /auto_examples/decomposition/images/sphx_glr_plot_incremental_pca_001.png :class: sphx-glr-multi-img * .. image-sg:: /auto_examples/decomposition/images/sphx_glr_plot_incremental_pca_002.png :alt: PCA of iris dataset :srcset: /auto_examples/decomposition/images/sphx_glr_plot_incremental_pca_002.png :class: sphx-glr-multi-img .. code-block:: Python # 作者:scikit-learn 开发者 # SPDX-License-Identifier: BSD-3-Clause import matplotlib.pyplot as plt import numpy as np from sklearn.datasets import load_iris from sklearn.decomposition import PCA, IncrementalPCA iris = load_iris() X = iris.data y = iris.target n_components = 2 ipca = IncrementalPCA(n_components=n_components, batch_size=10) X_ipca = ipca.fit_transform(X) pca = PCA(n_components=n_components) X_pca = pca.fit_transform(X) colors = ["navy", "turquoise", "darkorange"] for X_transformed, title in [(X_ipca, "Incremental PCA"), (X_pca, "PCA")]: plt.figure(figsize=(8, 8)) for color, i, target_name in zip(colors, [0, 1, 2], iris.target_names): plt.scatter( X_transformed[y == i, 0], X_transformed[y == i, 1], color=color, lw=2, label=target_name, ) if "Incremental" in title: err = np.abs(np.abs(X_pca) - np.abs(X_ipca)).mean() plt.title(title + " of iris dataset\nMean absolute unsigned error %.6f" % err) else: plt.title(title + " of iris dataset") plt.legend(loc="best", shadow=False, scatterpoints=1) plt.axis([-4, 4, -1.5, 1.5]) plt.show() .. rst-class:: sphx-glr-timing **Total running time of the script:** (0 minutes 0.105 seconds) .. _sphx_glr_download_auto_examples_decomposition_plot_incremental_pca.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_incremental_pca.ipynb :alt: Launch binder :width: 150 px .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: plot_incremental_pca.ipynb ` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: plot_incremental_pca.py ` .. container:: sphx-glr-download sphx-glr-download-zip :download:`Download zipped: plot_incremental_pca.zip ` .. include:: plot_incremental_pca.recommendations .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_