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).

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
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

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()

Total running time of the script: (0 minutes 8.795 seconds)

Gallery generated by Sphinx-Gallery