.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "auto_examples/basics/demo_tsne_swiss_roll.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_tsne_swiss_roll.py: TSNE embedding of the swiss roll dataset ======================================== We show how to compute a TSNE embedding with TorchDR on the swiss roll dataset. .. GENERATED FROM PYTHON SOURCE LINES 8-14 .. code-block:: Python # Author: RĂ©mi Flamary # Hugues Van Assel # # License: BSD 3-Clause License .. GENERATED FROM PYTHON SOURCE LINES 15-20 .. code-block:: Python import matplotlib.pyplot as plt from sklearn.datasets import make_swiss_roll from torchdr import TSNE .. GENERATED FROM PYTHON SOURCE LINES 21-25 Load toy images --------------- First, let's load swiss roll dataset from sklearn. .. GENERATED FROM PYTHON SOURCE LINES 25-30 .. code-block:: Python n_samples = 500 X, t = make_swiss_roll(n_samples=n_samples, noise=0.1, random_state=0) .. GENERATED FROM PYTHON SOURCE LINES 31-34 Plot the dataset ---------------- .. GENERATED FROM PYTHON SOURCE LINES 34-43 .. code-block:: Python fig = plt.figure(figsize=(8, 6)) ax = fig.add_subplot(111, projection="3d") fig.add_axes(ax) ax.scatter(X[:, 0], X[:, 1], X[:, 2], c=t, s=50, alpha=0.8) ax.set_title("Swiss Roll in Ambient Space") ax.view_init(azim=-66, elev=12) _ = ax.text2D(0.8, 0.05, s=f"n_samples={n_samples}", transform=ax.transAxes) .. image-sg:: /auto_examples/basics/images/sphx_glr_demo_tsne_swiss_roll_001.png :alt: Swiss Roll in Ambient Space :srcset: /auto_examples/basics/images/sphx_glr_demo_tsne_swiss_roll_001.png :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 44-46 Compute the TSNE embedding -------------------------- .. GENERATED FROM PYTHON SOURCE LINES 46-51 .. code-block:: Python tsne = TSNE(n_components=2, perplexity=10, max_iter=200) X_embedded = tsne.fit_transform(X) .. GENERATED FROM PYTHON SOURCE LINES 52-54 Plot the TSNE embedding ----------------------- .. GENERATED FROM PYTHON SOURCE LINES 54-60 .. code-block:: Python plt.figure(figsize=(8, 6)) plt.scatter(X_embedded[:, 0], X_embedded[:, 1], c=t, s=50, alpha=0.8) plt.title("TSNE embedding of the Swiss Roll dataset") .. image-sg:: /auto_examples/basics/images/sphx_glr_demo_tsne_swiss_roll_002.png :alt: TSNE embedding of the Swiss Roll dataset :srcset: /auto_examples/basics/images/sphx_glr_demo_tsne_swiss_roll_002.png :class: sphx-glr-single-img .. rst-class:: sphx-glr-script-out .. code-block:: none Text(0.5, 1.0, 'TSNE embedding of the Swiss Roll dataset') .. GENERATED FROM PYTHON SOURCE LINES 61-63 See the impact of perplexity ---------------------------- .. GENERATED FROM PYTHON SOURCE LINES 63-81 .. code-block:: Python perplexity_values = [2, 5, 10, 20] X_embedded = [] for perplexity in perplexity_values: if len(X_embedded) == 0: init = "pca" else: init = X_embedded[-1] tsne = TSNE(n_components=2, perplexity=perplexity, max_iter=200, init=init) X_embedded.append(tsne.fit_transform(X)) plt.figure(figsize=(12, 4)) for i, perplexity in enumerate(perplexity_values): plt.subplot(1, 4, i + 1) plt.scatter(X_embedded[i][:, 0], X_embedded[i][:, 1], c=t, s=50, alpha=0.8) plt.title(f"Perplexity = {perplexity}") .. image-sg:: /auto_examples/basics/images/sphx_glr_demo_tsne_swiss_roll_003.png :alt: Perplexity = 2, Perplexity = 5, Perplexity = 10, Perplexity = 20 :srcset: /auto_examples/basics/images/sphx_glr_demo_tsne_swiss_roll_003.png :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 82-87 We can observe that the perplexity parameter significantly influences the embedding. When the perplexity is too low, the embedding captures only short-range dependencies and fails to capture the manifold's geometry. Conversely, when the perplexity is too high, points that are distant on the manifold but close in the ambient space are mistakenly considered neighbors. .. rst-class:: sphx-glr-timing **Total running time of the script:** (1 minutes 6.261 seconds) .. _sphx_glr_download_auto_examples_basics_demo_tsne_swiss_roll.py: .. only:: html .. container:: sphx-glr-footer sphx-glr-footer-example .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: demo_tsne_swiss_roll.ipynb ` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: demo_tsne_swiss_roll.py ` .. container:: sphx-glr-download sphx-glr-download-zip :download:`Download zipped: demo_tsne_swiss_roll.zip ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_