Skip to main content

Module 7 — t-SNE and UMAP: reading projections with care

PCA is linear, and that constraint has a cost: it cannot unfold a coiled structure. t-SNE and UMAP can, and they produce spectacular visualizations where clusters appear cleanly separated. These images are valuable — and among the most misinterpreted in the entire discipline. This module teaches you as much to use them as to distrust them.

A different objective: preserving neighborhoods, not geometry

Where PCA preserves the directions of greatest variance, t-SNE and UMAP pursue an explicitly local goal: if two points are neighbors in high dimension, they must remain neighbors on the plot. No promise whatsoever about distant points.

That choice explains both their visual effectiveness and their traps. By sacrificing large distances, these methods gain all the room they need to separate local neighborhoods cleanly — hence very readable images. But by the same gesture, they render the plot's global geometry uninterpretable.

t-SNE in practice

t-SNE builds a notion of similarity between points in high dimension, then looks for a 2D layout reproducing those similarities.

from sklearn.manifold import TSNE
X_acp = PCA(n_components=50).fit_transform(X_s) # PCA first: standard practice
X_2d = TSNE(n_components=2, perplexity=30, random_state=42).fit_transform(X_acp)

Two points of method. The upstream PCA is not a detail: it denoises and considerably speeds up the computation, and it is standard practice on real data. Perplexity controls how many neighbors are taken into account (typically 5 to 50): a low value highlights microstructures, a high value broader groupings. It is strongly advised to produce several values and keep only what persists from one projection to the next.

UMAP, faster and better structured

UMAP answers the same question on different mathematical foundations, and offers three practical advantages that explain its wide adoption: it is markedly faster on large volumes, it preserves global structure somewhat better than t-SNE (without guaranteeing it), and it can project new points without recomputing everything — which t-SNE cannot. Its main parameters, n_neighbors (analogous to perplexity) and min_dist (cluster compactness), are explored the same way.

What a projection does not tell you

Here are the most frequent reading errors, and what to substitute for them:

What you think you readReality
"these two clusters are far apart, so very different"distances between clusters mean nothing
"this cluster is wider, so more dispersed"cluster sizes are not interpretable
"there are 5 groups, it's visible"the algorithm separates neighborhoods, even absent real structure
"I'll run my clustering on these 2 dimensions"avoid: the projection distorts distances

The last point deserves emphasis, because the error is tempting. Clustering on t-SNE coordinates means trusting distances the method never sought to preserve. The right sequence: cluster in the original space (or after PCA, which preserves distances), then use the projection only to color and visualize the resulting clusters.

The pure-noise test

Apply t-SNE to perfectly random data with no structure at all: you will often get convincing-looking clusters. The algorithm separates whatever neighborhoods it finds, meaningful or accidental. A projection therefore never proves that clusters exist; it suggests leads, to be confirmed by methods that do measure — silhouette, stability, business sense.

Good practice in three points

These methods are excellent for exploring an unfamiliar dataset, for visually checking a clustering obtained elsewhere, and for presenting a result compellingly. They are not a measurement tool, nor a preprocessing step. To compress, denoise or prepare a model, PCA remains the appropriate tool.

Summary

  • t-SNE and UMAP preserve local neighborhoods, not global geometry: the source of both their readability and all their traps.
  • Perplexity (t-SNE) and n_neighbors (UMAP) set the scale observed; vary these values and keep only what persists.
  • Distances between clusters, cluster sizes and the apparent number of clusters are not interpretable.
  • Cluster in the original space or after PCA, never on projection coordinates, which serve to visualize and present.

Next module: anomaly detection, the course's third family, with the isolation forest and statistical approaches.