.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "auto_examples/08_neighbor_embedding/plot_02_umap_tsne.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_08_neighbor_embedding_plot_02_umap_tsne.py: UMAP and t-SNE on handwritten digits ==================================== Neighbor-embedding methods build a weighted kNN graph and lay it out so that graph neighbors attract and random pairs repel. t-SNE uses Gaussian neighborhoods of fixed perplexity and heavy-tailed Student-t similarities in the map; UMAP uses a fuzzy union of local neighborhoods and a tunable curve. Both separate the ten digit classes far better than PCA or Isomap, at the price of distorting large distances (the arrangement of the clusters relative to each other is not reliable). .. GENERATED FROM PYTHON SOURCE LINES 13-34 .. image-sg:: /auto_examples/08_neighbor_embedding/images/sphx_glr_plot_02_umap_tsne_001.png :alt: PCA: T(10) = 0.830, C(10) = 0.951, Isomap: T(10) = 0.838, C(10) = 0.969, t-SNE: T(10) = 0.993, C(10) = 0.988, UMAP: T(10) = 0.989, C(10) = 0.985 :srcset: /auto_examples/08_neighbor_embedding/images/sphx_glr_plot_02_umap_tsne_001.png :class: sphx-glr-single-img .. rst-class:: sphx-glr-script-out .. code-block:: none PCA trust 0.830 cont 0.951 AUC 0.233 Isomap trust 0.838 cont 0.969 AUC 0.253 t-SNE trust 0.993 cont 0.988 AUC 0.537 UMAP trust 0.989 cont 0.985 AUC 0.411 | .. code-block:: Python import matplotlib.pyplot as plt from sklearn.decomposition import PCA from graphclust import Isomap, UMAPEmbedding, metrics, tsne from graphclust.datasets import load_digits from graphclust.plotting import plot_embedding X, y = load_digits() # [snippet:umap] umap = UMAPEmbedding(k=15, min_dist=0.1, random_state=0).fit(X) Y_tsne = tsne(X, perplexity=30, random_state=0) # [/snippet:umap] maps = {"PCA": PCA(2).fit_transform(X), "Isomap": Isomap(k=10).fit_transform(X), "t-SNE": Y_tsne, "UMAP": umap.embedding_} fig, axes = plt.subplots(1, 4, figsize=(14, 3.6)) for ax, (name, Y) in zip(axes, maps.items()): T, C = metrics.trustworthiness_continuity(X, Y, 10) plot_embedding(Y, y, ax=ax, s=3, title=f"{name}: T(10) = {T:.3f}, C(10) = {C:.3f}") print(f"{name:7s} trust {T:.3f} cont {C:.3f} AUC {metrics.auc_rnx(X, Y):.3f}") plt.tight_layout() .. rst-class:: sphx-glr-timing **Total running time of the script:** (0 minutes 8.795 seconds) .. _sphx_glr_download_auto_examples_08_neighbor_embedding_plot_02_umap_tsne.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/graphclust/main?urlpath=lab/tree/notebooks/auto_examples/08_neighbor_embedding/plot_02_umap_tsne.ipynb :alt: Launch binder :width: 150 px .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: plot_02_umap_tsne.ipynb ` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: plot_02_umap_tsne.py ` .. container:: sphx-glr-download sphx-glr-download-zip :download:`Download zipped: plot_02_umap_tsne.zip ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_