.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "auto_examples/02_visualization/plot_01_pca_vs_sammon.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_02_visualization_plot_01_pca_vs_sammon.py: PCA versus Sammon mapping: preserving variance or distances =========================================================== A clustering of :math:`n`-dimensional data cannot be inspected directly; the classical remedy is to project the data onto the plane. Principal component analysis (PCA) chooses the plane that retains as much *variance* as possible, which says nothing about the distances the clustering algorithm works with. Sammon's mapping instead places the :math:`N` points in the plane so that their pairwise distances :math:`d^*_{kl}` reproduce the original distances :math:`d_{kl}`; it minimizes the stress .. math:: E = \frac{1}{\lambda}\sum_{k 2 * h.min() else str(int(np.argmax(h <= h[-1] * 1.01))) print(f"{a:5.2f} {str(backtracking):12s} {run.stress_:12.4g} {n_within:>32s} {np.all(np.diff(h) <= 1e-12)}") print("\ncomponents PCA stress Sammon stress variance kept by PCA") for n_components in (1, 2, 3): Y_p, _, pca_n = pca_projection(X, n_components=n_components) run = Sammon(n_components=n_components, alpha=0.3, max_iter=100, init="pca").fit(X) print(f"{n_components:10d} {sammon_stress(X, Y_p):10.4f} {run.stress_:13.4f} {pca_n.explained_variance_ratio_.sum():20.2f}") # [/snippet:alpha_scan] .. rst-class:: sphx-glr-script-out .. code-block:: none alpha backtracking final stress iterations to within 1% of final monotone 0.05 True 0.03492 48 True 0.05 False 0.0447 59 False 0.10 True 0.03447 32 True 0.10 False 0.03728 59 False 0.30 True 0.03433 12 True 0.30 False 0.03432 29 False 0.50 True 0.03433 11 True 0.50 False 0.03431 19 False 0.90 True 0.03433 8 True 0.90 False 83.51 diverged False 1.20 True 0.03433 11 True 1.20 False 8745 diverged False components PCA stress Sammon stress variance kept by PCA 1 0.2036 0.1359 0.58 2 0.0673 0.0343 0.76 3 0.0167 0.0079 0.90 .. GENERATED FROM PYTHON SOURCE LINES 127-140 Take-away --------- PCA and Sammon mapping answer different questions. PCA keeps the directions of largest variance and is a linear map that can be applied to new data. Sammon mapping keeps the pairwise distances and yields a data-dependent configuration of :math:`N` points, faithful to what a distance-based clustering algorithm "sees" but not transferable to new samples. The Sammon stress is a genuine quality measure that can score any projection, including the PCA scores; here the Sammon map halves the stress of the PCA picture. The iteration is a diagonal Newton method whose step size matters only without the safeguard: with backtracking any :math:`\alpha` works, and the PCA start removes the dependence on a random initialization. .. rst-class:: sphx-glr-timing **Total running time of the script:** (0 minutes 1.891 seconds) .. _sphx_glr_download_auto_examples_02_visualization_plot_01_pca_vs_sammon.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/ComplexSystemsIntelligence/fuzzyclust/main?urlpath=lab/tree/notebooks/auto_examples/02_visualization/plot_01_pca_vs_sammon.ipynb :alt: Launch binder :width: 150 px .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: plot_01_pca_vs_sammon.ipynb ` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: plot_01_pca_vs_sammon.py ` .. container:: sphx-glr-download sphx-glr-download-zip :download:`Download zipped: plot_01_pca_vs_sammon.zip ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_