MODELCATALOG

Non-linear embedding, for looking at data.

sklearn.manifold

manifold

Non-linear embedding, for looking at data.

1 Get data Something to practise on
2 Split & tune An honest test set
3 Prepare Into numbers a model accepts
4 Compose One object, no leakage
5 Model The algorithm itself
6 Unsupervised No labels available
7 Measure Is it any good, and why
8 Utilities Glue and extension

What it is for

Projecting high-dimensional data to two dimensions so you can plot it. For visualisation only.

Contents

TSNE The common choice for plotting clusters
Isomap, LocallyLinearEmbedding, MDS, SpectralEmbedding Alternatives with different distance assumptions

Worked examples 1

Runnable, with the arguments that change the outcome called out. Output shapes are shown as comments where they help.

t-SNE for looking, not for measuring

Reduce with PCA first — t-SNE on raw high-dimensional data is slow and noisier.

from sklearn.decomposition import PCA
from sklearn.manifold import TSNE
import matplotlib.pyplot as plt

X_pca = PCA(n_components=50, random_state=42).fit_transform(X_scaled)

emb = TSNE(
    n_components=2,
    perplexity=30,        # roughly "how many neighbours matter"; 5-50
    learning_rate="auto",
    init="pca",           # more stable than random init
    random_state=42,
).fit_transform(X_pca)

plt.scatter(emb[:, 0], emb[:, 1], c=y, s=4, alpha=0.6, cmap="coolwarm")

# Three things this plot CANNOT tell you:
#   - cluster sizes (t-SNE expands sparse regions)
#   - distances between clusters (not meaningful)
#   - whether clusters are real (changing perplexity changes them)
# Use it to generate hypotheses, then test them on the real features.

Worth knowing

  1. t-SNE distances are not meaningful. Do not cluster on t-SNE output, and do not read cluster sizes or between-cluster distances off the plot.

  2. UMAP, which many prefer, is a separate library rather than part of sklearn.