SafeZone AI Learn
Learn/ Machine Learning/ Unsupervised Learning · lesson 4 of 7

t-SNE, UMAP and reading embedding plots

What the KL objective actually preserves, perplexity as a neighborhood dial, t-SNE running live on data whose true structure you know — and the definitive list of what cluster sizes and distances in these plots do NOT mean.

Every ML team’s slide deck contains one: a cloud of colored dots, “the embedding space”, clusters neatly separated, distances apparently meaningful. These plots are made by t-SNE or UMAP, and they are simultaneously the most useful exploratory tool in high-dimensional data analysis and the most systematically misread artifact in the field. This lesson derives what the algorithms optimize — because only the objective tells you what the picture can and cannot say — and then runs t-SNE live on data whose ground truth you know, so you can catch the distortions in the act.

Linear reduction, recapped in one paragraph

PCA (Foundations: Projections + SVD) finds the orthogonal directions of maximal variance — the best linear view, preserving large-scale geometry: distances, angles, densities are distorted only by dropping components. Its weakness is the mirror of its guarantee: a nonlinear manifold (a swiss roll, a cluster wrapped around another) has no good linear shadow. When PCA’s 2-D view explains little variance or shows structureless overlap, the nonlinear tools earn their turn — at the price of giving up exactly the geometric faithfulness PCA guaranteed.

t-SNE: matching neighbor distributions

t-SNE never tries to preserve distances. It converts them to neighbor probabilities and matches those. In the original space, point jj‘s probability of being picked as ii‘s neighbor uses a Gaussian kernel:

pji=exp(xixj2/2σi2)liexp(xixl2/2σi2),pij=pji+pij2n,p_{j|i} = \frac{\exp(-\lVert x_i - x_j \rVert^2 / 2\sigma_i^2)} {\sum_{l \neq i} \exp(-\lVert x_i - x_l \rVert^2 / 2\sigma_i^2)}, \qquad p_{ij} = \frac{p_{j|i} + p_{i|j}}{2n},

where each σi\sigma_i is tuned per point so the conditional’s entropy hits a target — the perplexity, effectively “how many neighbors count”. In the 2-D map, similarities use a Student-t kernel qij(1+yiyj2)1q_{ij} \propto (1 + \lVert y_i - y_j \rVert^2)^{-1} — heavy tails that let dissimilar points sit far apart without crushing gradients (the fix that made the method work). The map is found by gradient descent on

KL(PQ)=ijpijlogpijqij.\mathrm{KL}(P \,\|\, Q) = \sum_{i \neq j} p_{ij} \log \frac{p_{ij}}{q_{ij}}.

Read the objective the way the MLE lesson taught: KL punishes putting close points far apart (large pp, small qq) much harder than the reverse. So t-SNE preserves local neighborhoods and treats everything else — global distances, cluster sizes, densities — as negotiable. Every misreading of these plots is a failure to know that sentence.

Run it. The three clusters separate — neighborhoods preserved, the algorithm’s actual promise, kept. Now audit what it never promised. The GREEN cluster is 2× more spread than the others in 10-D, but in the map its apparent size is mostly an artifact of equalization — t-SNE expands dense clusters and contracts diffuse ones. The gaps BETWEEN islands carry almost no meaning: re-run and watch clusters land in different relative positions at similar KL. And drop perplexity to 3: clusters fragment into spurious islets (neighborhood too small to bridge sampling gaps); push it to 50 — nearly half the dataset — and structure starts to blur together. Same data, four different stories. The plot is a statement about the OBJECTIVE as much as about the data.

UMAP, and how it differs

UMAP builds a weighted k-nearest-neighbor graph, interprets it through fuzzy simplicial sets, and optimizes a cross-entropy that — unlike t-SNE’s KL — has an attractive and repulsive term for far pairs too. Practical differences: UMAP is faster, scales better, has n_neighbors (≈ perplexity) and min_dist (visual packing) as its dials, and tends to preserve slightly more global structure. What it does not do is escape the fundamental trade: it is still a neighbor-graph method, cluster sizes and inter-cluster distances remain unreliable, and its apparent global structure is sensitive to initialization (as is t-SNE’s — modern practice initializes both with PCA for stability, which also makes runs reproducible).

The reader’s contract

What you may and may not conclude from a t-SNE/UMAP plot, condensed:

  • May: points in the same island are likely neighbors in the original space; well-separated islands usually reflect real separation; local neighborhood browsing (“what sits next to what”) is the intended use.
  • May not: compare island SIZES (equalization artifact), read meaning into DISTANCES between islands, interpret DENSITY differences, or treat axis directions as meaningful (there are none — the objective is invariant to rotation).
  • Must: try several perplexities/n_neighbors before believing any structure; check suspected clusters with a method that outputs labels and uncertainty (GMM, HDBSCAN) in the ORIGINAL space; and remember that clustering on the 2-D embedding inherits its artifacts — cluster in more dimensions than you visualize.

One more honest note: distortion is not a fixable bug. A theorem-level fact (no embedding of most high-dimensional metrics into 2-D preserves all distances — Johnson–Lindenstrauss needs O(logn/ε2)O(\log n / \varepsilon^2) dimensions) means every 2-D picture of high-dimensional data lies about something. The only question is whether you know what yours is lying about.

import numpy as np
from sklearn.datasets import load_digits
from sklearn.decomposition import PCA
from sklearn.manifold import TSNE

X, y = load_digits(return_X_y=True)          # 64-D, 10 true classes
Z = TSNE(perplexity=30, init="pca", random_state=0).fit_transform(X)
# rerun with perplexity 5 and 100; overlay y as colors; note what changes
# and what survives — survivors are the trustworthy structure
print(PCA(2).fit(X).explained_variance_ratio_.sum())   # linear view: ~29%

Exercises

Work these before the next lesson

  1. Derive the asymmetry: expand KL(P‖Q) for one pair and show the cost of (p large, q small) vs (p small, q large). Conclude in one sentence what t-SNE preserves and what it sacrifices.
    Solution

    Worked solutions are part of Premiumunlock all of them for £5/month →

  2. 5 more exercises — each with a worked solution — are part of Premium. Unlock everything for £5/month →

References

  • L. van der Maaten & G. Hinton, “Visualizing Data using t-SNE”, JMLR 2008.
  • L. McInnes, J. Healy & J. Melville, “UMAP: Uniform Manifold Approximation and Projection”, 2018.
  • M. Wattenberg, F. Viégas & I. Johnson, “How to Use t-SNE Effectively”, Distill 2016 — the interactive companion to this lesson’s warnings.
  • D. Kobak & P. Berens, “The art of using t-SNE for single-cell transcriptomics”, Nat. Comm. 2019 — best practices, incl. PCA init.