.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "auto_examples/basics/demo_pca_via_affinity_matcher.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. .. rst-class:: sphx-glr-example-title .. _sphx_glr_auto_examples_basics_demo_pca_via_affinity_matcher.py: PCA via SVD and via AffinityMatcher =================================== We show how to compute a PCA embedding using the closed form and using the AffinityMatcher class. Both approaches lead to the same solution. .. GENERATED FROM PYTHON SOURCE LINES 9-20 .. code-block:: Python # Author: Hugues Van Assel # # License: BSD 3-Clause License import matplotlib.pyplot as plt from sklearn.datasets import load_digits from torchdr import AffinityMatcher, ScalarProductAffinity from torchdr.spectral import PCA .. GENERATED FROM PYTHON SOURCE LINES 21-25 Load toy images --------------- First, let's load 5 classes of the digits dataset from sklearn. .. GENERATED FROM PYTHON SOURCE LINES 25-30 .. code-block:: Python digits = load_digits(n_class=5) X = digits.data X = X - X.mean(0) .. GENERATED FROM PYTHON SOURCE LINES 31-37 PCA via SVD ----------- Let us perform PCA using the closed form solution given by the Singular Value Decomposition (SVD). In ``Torchdr``, it is available at :class:`torchdr.PCA`. .. GENERATED FROM PYTHON SOURCE LINES 37-46 .. code-block:: Python Z_svd = PCA(n_components=2).fit_transform(X) plt.figure() plt.scatter(Z_svd[:, 0], Z_svd[:, 1], c=digits.target) plt.title("PCA via SVD") plt.show() .. image-sg:: /auto_examples/basics/images/sphx_glr_demo_pca_via_affinity_matcher_001.png :alt: PCA via SVD :srcset: /auto_examples/basics/images/sphx_glr_demo_pca_via_affinity_matcher_001.png :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 47-56 PCA via AffinityMatcher ----------------------- Now, let us perform PCA using the AffinityMatcher class :class:`torchdr.AffinityMatcher` as well as the scalar product affinity :class:`torchdr.ScalarProductAffinity` for both input data and embeddings, and the square loss as global objective. .. GENERATED FROM PYTHON SOURCE LINES 56-75 .. code-block:: Python model = AffinityMatcher( n_components=2, affinity_in=ScalarProductAffinity(), affinity_out=ScalarProductAffinity(), loss_fn="square_loss", init="normal", lr=1e1, max_iter=50, backend=None, ) Z_am = model.fit_transform(X) plt.figure() plt.scatter(Z_am[:, 0], Z_am[:, 1], c=digits.target) plt.title("PCA via AffinityMatcher") plt.show() .. image-sg:: /auto_examples/basics/images/sphx_glr_demo_pca_via_affinity_matcher_002.png :alt: PCA via AffinityMatcher :srcset: /auto_examples/basics/images/sphx_glr_demo_pca_via_affinity_matcher_002.png :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 76-78 We can see that we obtain the same PCA embedding (up to a rotation) using both methods. .. rst-class:: sphx-glr-timing **Total running time of the script:** (0 minutes 4.218 seconds) .. _sphx_glr_download_auto_examples_basics_demo_pca_via_affinity_matcher.py: .. only:: html .. container:: sphx-glr-footer sphx-glr-footer-example .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: demo_pca_via_affinity_matcher.ipynb ` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: demo_pca_via_affinity_matcher.py ` .. container:: sphx-glr-download sphx-glr-download-zip :download:`Download zipped: demo_pca_via_affinity_matcher.zip ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_