t-SNE and UMAP beside PCA

Layouts that preserve who is near whom, at the cost of making the distance between clusters meaningless.

Overview

A note on what this page runs

The layout here is a simplified neighbour-preserving optimisation — attract each point toward its nearest neighbours in the original space, repel everything else. It is not t-SNE: there is no perplexity, no Student-t kernel, no KL divergence being minimised.

What it reproduces faithfully is the behaviour that matters: local structure is preserved, and the distances between groups stop meaning anything. The [PCA module](pca.html) covers the linear method properly.

t-SNE and UMAP beside PCA

This module needs JavaScript: the numbers are computed in the page rather than recorded.

Worth knowing

PCA is a linear projection: it keeps the directions of greatest variance, so global distances survive.
t-SNE and UMAP optimise a layout so that near neighbours stay near. Nothing constrains anything else.
The distance between clusters in a t-SNE plot carries no information. Neither does cluster size.
The layout on this page is a simplified neighbour-preserving optimisation, not t-SNE itself — it shows the behaviour, not the algorithm.

t-SNE and UMAP beside PCA

Why neighbour-preserving plots look so much better than PCA, and which parts of them you are not allowed to read.

Two different jobs

PCA finds the directions along which the data varies most and projects onto them. It is linear, deterministic, invertible, and fast — and because it is a projection, distances survive it in a predictable way. Points far apart in the original space are far apart in the plot, up to the variance the discarded dimensions held.

t-SNE and UMAP do something else entirely. They ask which points are near each other in the original space, and then *construct* a two-dimensional arrangement in which those same points are near each other. There is no projection and no formula mapping one space to the other — the coordinates are the output of an optimisation.

What the readout is checking

The data behind this page has three clusters arranged on a line, with the third exactly twice as far from the first as the second is. A ratio of 2.00, by construction.

Switch between the methods and watch the measured ratio.

PCA reproduces it closely. It is a linear projection, so the global arrangement is preserved.

The neighbour-preserving layout does not, and the number wanders as you change the attraction strength. The three groups are cleanly separated — visibly better than PCA — and the distances between them are an artefact of the optimisation.

That is the whole lesson, and it is the thing most often got wrong when reading these plots.

What you may not read from a t-SNE plot

Distance between clusters. Two clusters at opposite ends may be more similar than two that are adjacent. The layout only tried to keep neighbours together.

Cluster size. t-SNE expands dense regions and contracts sparse ones, so the area a cluster occupies says nothing about how many points it has or how spread out they were.

Density within a cluster. Same reason.

Anything from one run. t-SNE is stochastic and its result depends on the random seed and on perplexity. Run it three times and you get three pictures. Structure that survives all three is real; structure that does not, is not.

Perplexity and n_neighbors

Both algorithms have a parameter controlling how much neighbourhood to consider — perplexity in t-SNE, n_neighbors in UMAP. Low values emphasise very local structure and can shatter a genuine cluster into fragments. High values emphasise broader structure and can merge distinct clusters.

There is no correct value, and the standard advice is to look at several. A finding that only appears at one setting is a finding about the parameter.

t-SNE against UMAP

Speed. UMAP is substantially faster and scales to larger datasets.

Global structure. UMAP claims to preserve more of it, and generally does, though the caution above still applies — less meaningless is not meaningful.

New points. UMAP can transform data it did not see during fitting. t-SNE cannot: adding a point means re-running everything. This matters if the embedding is part of a pipeline rather than a picture.

Reproducibility. Both are stochastic; both take a seed.

What they are for

Looking. These are visualisation tools, and they are very good at answering "does my data separate at all", "are my labels consistent", "is there a group I did not expect".

They are a poor choice as a preprocessing step for a model. The output coordinates have no meaning outside the plot, the mapping is not stable, and for t-SNE it cannot be applied to new data at all. If you want dimensionality reduction inside a pipeline, PCA is the safe default.

Where it goes wrong

Measuring distance between clusters. The single most common error.

Reading cluster sizes. Artefacts of the optimisation.

Running once and believing it. Vary the seed and the perplexity.

Feeding t-SNE coordinates into a classifier. No stable mapping; no way to transform new data.

Check yourself

0 of 3

Answer without scrolling back up.

  1. Why can you not read the distance between two clusters in a t-SNE plot?

  2. What can UMAP do that t-SNE cannot?

  3. Why is PCA the safer choice for dimensionality reduction inside a pipeline?

Cheat sheet

t-SNE and UMAP beside PCA

The layout here is a simplified neighbour-preserving optimisation — attract each point toward its nearest neighbours in the original space, repel everything else. It is not t-SNE: there is no perplexity, no Student-t kernel, no KL divergence being minimised.

MACHINE LEARNING · vizlearn.in/machine_learning/tsne_and_umap.html

About the author

Ashish Jangra builds and maintains VizLearn. Every module here is written and the visualisation behind it hand-built, so the numbers in a readout come from the same code that draws the picture. Corrections are genuinely welcome and get priority over everything else — if a page states something wrong, or an animation misrepresents what the algorithm does, get in touch.