Maps of high-dimensional data: t-SNE and UMAP
Last updated
PCA flattens data onto its two most spread-out directions, and on the digits the ten classes land in a tangle. t-SNE and UMAP make a different kind of map: they try to keep each point's nearest neighbours near it, and let everything else go. The result shows clusters beautifully, and it is very easy to misread. This lesson makes the maps and then learns what they can't say.
By the end of this lesson you can
- Make a 2-D t-SNE map of the digits, and compare it with PCA
- Explain what t-SNE keeps (neighbours) and what it throws away (distances between clusters, sizes)
- Set perplexity, and describe how UMAP differs
- Read these maps without over-reading them
Neighbours in high dimensions
Six hundred digits, 64 numbers each, put on a page two ways:
On the left, PCA's two biggest directions (lesson 4.10): a few digits stand apart, and most of the others overlap in the middle. Two straight directions can't hold ten classes. On the right, t-SNE: separate islands, nearly one per digit, with a few digits stranded on another's island (often a badly written one, worth looking at).
t-SNE was given no labels. The colours were added afterwards. It found the islands because digits of the same class are each other's nearest neighbours in 64-D, and t-SNE's only aim is to keep neighbours near each other on the page.
What t-SNE keeps
t-SNE (t-distributed stochastic neighbour embedding; van der Maaten and Hinton, 2008) works in three steps:
- In the original space, turn distances into similarities: for each point , a probability that it would pick as its neighbour, high for close points and falling off fast with distance, like a bell curve centred on .
- On the page, start with random positions, and work out similarities the same way, but with a long-tailed curve (Student's ) instead of a bell.
- Move the points on the page by gradient descent (lesson 3.3) to make the s match the s, minimising the KL divergence (lesson 1.11) between them.
Watch it settle, on 150 points from five clusters in 10-D:
The start: 150 random dots, coloured by the cluster each came from in 10-D. t-SNE never sees the colours.
The KL divergence punishes putting true neighbours far apart heavily, and putting far-apart points close together only lightly. So t-SNE works hard to keep each neighbourhood together, and hardly at all on how far apart the neighbourhoods are. The long tail of the curve then lets separate clusters drift well apart on the page, which is what makes the islands so clear.
Perplexity
How many neighbours count? The perplexity, roughly the number of near neighbours each point pays attention to:
With perplexity 3, each point only cares about a couple of others, and the map breaks into many small clumps. At 30, the default, the digits form ten clear islands. At 100, each island takes in more, and their shapes round out. There's no right value; the advice is to try several, from about 5 to 50, and trust only what they agree on. The map also changes with the random start, so try a few seeds too.
Wrong way: reading the distances between clusters
Three clusters in 10-D: A and B are 3 apart, and C is 30 away from A, ten times further. On a t-SNE map:
In 10-D, C is ten times as far from A as B is. On the map, about one and a half times. The map kept each cluster together and put them all at roughly similar distances. If you'd concluded from the map that A, B and C are about equally different, you'd be wrong.
The same goes for sizes: t-SNE expands dense clusters and shrinks sparse ones, so a big island doesn't mean a big spread. And for shapes: a long island needn't be a long cluster. The only thing to read from a t-SNE map is which points are near which.
UMAP
UMAP (uniform manifold approximation and projection; McInnes and others, 2018) is newer and makes similar maps from a similar idea: build a graph linking each point to its nearest neighbours, then lay the graph out in 2-D so linked points stay close. Compared with t-SNE, it's much faster on large data, keeps somewhat more of the large-scale layout, and can map new points onto an existing map. Its main setting, n_neighbors, plays the part of perplexity.
It isn't part of scikit-learn, and the course's in-page Python can't install it. On your own computer:
# pip install umap-learn
import umap
embedding = umap.UMAP(n_neighbors=15, min_dist=0.1, random_state=0).fit_transform(X)fit_transform returns the 2-D positions, like TSNE's. All the warnings in step 4 apply to UMAP too.
How to read these maps
What a t-SNE or UMAP map can tell you:
- Which points are similar: neighbours on the map were usually neighbours in the data.
- That there are groups, worth looking at, and which examples sit in an unexpected group.
What it can't, by itself:
- How far apart the groups are, how big they are, or what shape.
- That the groups are real: random data with no structure can still form clumps at low perplexity.
And the maps are for looking, not for modelling. The positions change with the seed and the settings, and t-SNE has no way to place a new point. To compress features for a model, use PCA (lesson 4.10).
Check what you learned
Sources
Every example on this page was run with Python 3.14, and its output and error messages are copied from it. These pages cover the same ground in more depth.
- McInnes, Healy and Melville, UMAP: uniform manifold approximation and projection for dimension reduction, 2018: UMAP
- Wattenberg, Viégas and Johnson, How to use t-SNE effectively, Distill, 2016: what the maps show and don’t, with live examples
- scikit-learn: t-distributed Stochastic Neighbor Embedding: TSNE and its perplexity