.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "auto_examples/decomposition/plot_varimax_fa.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_varimax_fa.py: =============================================================== 因子分析(带旋转)以可视化模式 =============================================================== 研究鸢尾花数据集,我们发现花萼长度、花瓣长度和花瓣宽度高度相关。花萼宽度的冗余性较低。矩阵分解技术可以揭示这些潜在模式。对结果组件应用旋转并不会本质上提高衍生潜在空间的预测价值,但可以帮助可视化它们的结构;例如,这里的方差最大化旋转,通过最大化权重的平方方差找到一种结构,其中第二个组件仅在花萼宽度上正加载。 .. GENERATED FROM PYTHON SOURCE LINES 9-20 .. code-block:: Python # 作者:scikit-learn 开发者 # SPDX 许可证标识符:BSD-3-Clause import matplotlib.pyplot as plt import numpy as np from sklearn.datasets import load_iris from sklearn.decomposition import PCA, FactorAnalysis from sklearn.preprocessing import StandardScaler .. GENERATED FROM PYTHON SOURCE LINES 21-23 %% 加载鸢尾花数据 .. GENERATED FROM PYTHON SOURCE LINES 23-28 .. code-block:: Python data = load_iris() X = StandardScaler().fit_transform(data["data"]) feature_names = data["feature_names"] .. GENERATED FROM PYTHON SOURCE LINES 29-30 绘制鸢尾花特征的协方差 .. GENERATED FROM PYTHON SOURCE LINES 30-43 .. code-block:: Python ax = plt.axes() im = ax.imshow(np.corrcoef(X.T), cmap="RdBu_r", vmin=-1, vmax=1) ax.set_xticks([0, 1, 2, 3]) ax.set_xticklabels(list(feature_names), rotation=90) ax.set_yticks([0, 1, 2, 3]) ax.set_yticklabels(list(feature_names)) plt.colorbar(im).ax.set_ylabel("$r$", rotation=0) ax.set_title("Iris feature correlation matrix") plt.tight_layout() .. image-sg:: /auto_examples/decomposition/images/sphx_glr_plot_varimax_fa_001.png :alt: Iris feature correlation matrix :srcset: /auto_examples/decomposition/images/sphx_glr_plot_varimax_fa_001.png :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 44-45 进行带有方差最大化旋转的因子分析 .. GENERATED FROM PYTHON SOURCE LINES 45-73 .. code-block:: Python n_comps = 2 methods = [ ("PCA", PCA()), ("Unrotated FA", FactorAnalysis()), ("Varimax FA", FactorAnalysis(rotation="varimax")), ] fig, axes = plt.subplots(ncols=len(methods), figsize=(10, 8), sharey=True) for ax, (method, fa) in zip(axes, methods): fa.set_params(n_components=n_comps) fa.fit(X) components = fa.components_.T print("\n\n %s :\n" % method) print(components) vmax = np.abs(components).max() ax.imshow(components, cmap="RdBu_r", vmax=vmax, vmin=-vmax) ax.set_yticks(np.arange(len(feature_names))) ax.set_yticklabels(feature_names) ax.set_title(str(method)) ax.set_xticks([0, 1]) ax.set_xticklabels(["Comp. 1", "Comp. 2"]) fig.suptitle("Factors") plt.tight_layout() plt.show() .. image-sg:: /auto_examples/decomposition/images/sphx_glr_plot_varimax_fa_002.png :alt: Factors, PCA, Unrotated FA, Varimax FA :srcset: /auto_examples/decomposition/images/sphx_glr_plot_varimax_fa_002.png :class: sphx-glr-single-img .. rst-class:: sphx-glr-script-out .. code-block:: none PCA : [[ 0.52106591 0.37741762] [-0.26934744 0.92329566] [ 0.5804131 0.02449161] [ 0.56485654 0.06694199]] Unrotated FA : [[ 0.88096009 -0.4472869 ] [-0.41691605 -0.55390036] [ 0.99918858 0.01915283] [ 0.96228895 0.05840206]] Varimax FA : [[ 0.98633022 -0.05752333] [-0.16052385 -0.67443065] [ 0.90809432 0.41726413] [ 0.85857475 0.43847489]] .. rst-class:: sphx-glr-timing **Total running time of the script:** (0 minutes 0.239 seconds) .. _sphx_glr_download_auto_examples_decomposition_plot_varimax_fa.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_varimax_fa.ipynb :alt: Launch binder :width: 150 px .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: plot_varimax_fa.ipynb ` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: plot_varimax_fa.py ` .. container:: sphx-glr-download sphx-glr-download-zip :download:`Download zipped: plot_varimax_fa.zip ` .. include:: plot_varimax_fa.recommendations .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_