TL;DR
UMAP is a method for drawing high-dimensional data in two or three dimensions. It first records which points are near each other, with a weight for each neighbour, and then moves the points in the picture until the picture has the same neighbour pattern. Points that were close in the data end up close on the plot, and the method is fast enough for large datasets.
Builds on: Dimensionality Reduction, K-Nearest Neighbors, Fuzzy Set, Cross-Entropy Loss · Leads to: UMAP: Uniform Manifold Approximation and Projection for Dimension Reduction
Where it's used
Single-cell biology. Experiments measure tens of thousands of genes per cell, and researchers plot the cells to see cell types. UMAP is a common step in these pipelines, because the plot shows groups of cells that the gene data alone hides.
Checking embeddings. A language or image model turns each input into a Vector of a few hundred numbers. Engineers use UMAP to plot those vectors and see whether similar inputs, such as one class of images, form a visible group. The plot is a diagnostic, not a measurement.
Preprocessing for clustering. Clustering in 1,000 dimensions is slow and unreliable, since distances there are less informative. Projecting first to 10 or 20 dimensions with UMAP, then clustering, is a standard recipe. This keeps the local neighbourhoods that the clustering step needs.
How it works
Take a tiny dataset of four points on a line: A at 0, B at 1, C at 2 and D at 10. The distances are 1 (A to B), 2 (A to C), 1 (B to C), and so on. D is far away from the rest. The aim is a picture in which the nearby points stay near and D stays apart. Use neighbours per point, found as in K-Nearest Neighbors, and use a fixed so the arithmetic stays simple. The real method chooses per point, as the math section explains.
- Find each point's two nearest neighbours. A's are B (distance 1) and C (distance 2). B's are A and C (both distance 1). C's are B (distance 1) and A (distance 2). D's are C (distance 8) and B (distance 9). Check: D to C is 10 minus 2, which is 8.
- Give each point a local scale. Let be the distance from point to its nearest neighbour. So , , , . The nearest neighbour always gets weight 1, which guarantees that every point is connected to something.
- Turn distances into weights. For a neighbour of , the weight is , where is the distance and is a per-point scale. Take for this illustration. The weight of D to C is . Check: , so the exponent is zero.
- Make the graph symmetric. A may count B as a neighbour while B does not count A. The union rule combines the two directions: . For A and B, both directions are 1, so . Check: a weight of 1 in either direction gives 1 overall.
- Lay out the points so the picture has the same weights. Each pair in the picture gets a target closeness that falls with distance. We place points in two dimensions so that the closeness of each pair matches its weight . A pair with should be almost touching, and a pair with a small should be far apart.
- Check the result. In the picture, A-B and B-C should be close, A-C a little farther (weight 0.6), and D close to C but with its weight to B (0.368) pulling it only weakly toward the group. Check: the picture is right if the ordering of closeness matches the ordering of weights.
The whole procedure is a pipeline:
flowchart LR A["High-dimensional points"] --> B["k-nearest-neighbour lists"] B --> C["Local scales rho and sigma"] C --> D["Fuzzy weights, symmetrised"] D --> E["Initial 2-D layout"] E --> F["Move points to match the weights"] F --> G["Plot"]
The math
The symmetric weight combines the two directions, using the rule for the union of two fuzzy sets in Fuzzy Set:
The scale is not chosen by hand. It is the value that makes the weights of point add up to , which keeps the total "amount of neighbourhood" the same for every point regardless of density. Here is the base-2 logarithm of .
Step 1 in symbols
The neighbour lists are the smallest values of for each . In the walkthrough, , and .
Steps 2 and 3 in symbols
With and , the weight of D to C is . The weight of D to B, at distance 9, is .
Step 4 in symbols
For the pair A and C, each list contains the other, with . The formula gives , the weight 0.6 used in step 6. If only one list contained the other, the weight would be just .
Step 5 in symbols
The low-dimensional closeness of two points and in the picture is
where and are constants fitted from the chosen minimum distance min_dist. This is close to 1 for nearby points and falls toward 0 for distant ones. The layout is chosen to make close to , by minimising the cross-entropy between the two sets of weights:
The first term pulls pairs with high together. The second pushes apart pairs with low . In practice the second sum is approximated by sampling a few random non-neighbours for each point, which is the negative sampling step that makes the method fast.
Step 6 in symbols
A quick check is to compute the loss for one pair. The next worked example does this.
Worked examples
- Point has nearest neighbour at distance , so .
- Another neighbour is at distance . Take .
- Weight of from : .
- Compute: .
- Interpretation: is two units farther than the nearest neighbour, so its weight drops from 1 to about 0.135.
Try it: a third point is at distance 2 from , with and . What is its weight, and is it bigger or smaller than the weight of ? Answer: . It is bigger than the weight 0.135 of , because is closer.
Suppose a pair has symmetric weight . We compare three candidate closenesses for that pair in the picture, using one term of the cross-entropy, .
- For : .
- For : .
- For : .
- Compare: 0.673 is the smallest, and it occurs at .
- Convert to a distance with , a simplified choice for hand calculation: gives , so .
- Interpretation: the layout should place this pair about 0.82 units apart. A pair that should be closer, with , would need , and in practice
min_distsets how tightly the points may pack.
Try it: a pair with is placed so that . Using , what distance should separate them? Answer: gives , so . A weak neighbour belongs about two and a half times as far away as the 0.82 of the previous step.
Take a point whose nearest neighbours are at distances 2.0, 2.5 and 3.0 in a high-dimensional space. The nearest distance is . The target total weight is . The weights are for the nearest neighbour, for the one at 2.5, and for the one at 3.0. We need the scale that makes the sum match.
- Equation to solve: , so .
- Try : . This is too small.
- Try : . This is too big.
- Try : . This is close enough to 0.585.
- So the point's scale is about .
- Interpretation: a dense region gets a small scale, so its neighbours still get weights that fall off quickly with distance. A sparse point gets a larger scale, so its neighbours keep useful weights. This is what lets UMAP handle clusters of different densities in one picture.
Try it: with the same distances and , what is the weight of the neighbour at distance 3.0? Answer: . The farthest neighbour has the smallest weight, as expected.
Common mistakes and tradeoffs
It is tempting to think distances in the picture match distances in the data. In fact the layout only tries to match the neighbour weights. Long distances and the size of groups in the picture are not reliable measurements, and a tight-looking group may hide structure.
It is tempting to think each run gives the same picture. In fact the optimisation starts from a random or spectral initial layout and uses random samples for negative sampling. Fix a random seed when you need reproducible plots, and expect runs to differ in orientation and in the placement of small groups.
It is tempting to think n_neighbors and min_dist are cosmetic. In fact [[k-nearest-neighbors|n_neighbors]] sets how local the view is: a small value shows fine detail, a large value shows the broad shape. min_dist sets how tightly points may pack in the picture, so a small value makes clumps and a large value spreads them out.
UMAP is a good default when the data is large, when you want a layout that keeps neighbourhoods, and when you may want the embedding for clustering later. It is not a substitute for a measurement. t-SNE is an older method with a similar goal, and it is often slower on large data. Principal Component Analysis is linear and keeps global variance, so it is the right choice when the structure is roughly flat. Use UMAP's output as a picture or as input to another step, and check it against a known label before trusting it.
History
Methods that build a graph of neighbours and embed it go back at least to Isomap in 2000 and Laplacian eigenmaps in 2003, which use the graph Laplacian (Graph Laplacian) to find a low-dimensional layout. Laurens van der Maaten and Geoffrey Hinton's t-SNE in 2008 made neighbourhood plots popular, but it was slow on large data and tended to lose global layout. In 2018 Leland McInnes, John Healy and James Melville introduced UMAP, which builds a fuzzy version of the neighbour graph and optimises a cross-entropy, as in UMAP: Uniform Manifold Approximation and Projection for Dimension Reduction. It caught on quickly in biology and machine learning, because it was fast and produced clear pictures of large datasets.
Recap
- UMAP starts from each point's nearest neighbours and gives each neighbour a weight that falls with distance.
- A local scale per point makes the weights comparable across dense and sparse regions.
- Neighbour weights are symmetrised with the fuzzy union rule, giving a single weighted graph.
- A low-dimensional layout is chosen to match those weights by minimising a cross-entropy, using attraction for neighbours and sampled repulsion for the rest.
- Read the picture as a map of neighbourhoods, not of exact distances.
Read next: UMAP: Uniform Manifold Approximation and Projection for Dimension Reduction for the original method and its experiments, and Fuzzy Simplicial Set for the topological view behind the weights.
Check yourself
QuestionRecall: what does the nearest neighbour of a point always get as its weight in UMAP, and why?Show answer
QuestionComputation: a neighbour is at distance 4.0, the nearest distance is , and . What is its directed weight?Show answer
QuestionWhy: why does UMAP use a per-point scale instead of one global distance threshold?Show answer
QuestionApplied: you plot UMAP of cells and see two groups far apart in the picture. Can you conclude the groups are twice as different as two groups drawn closer together?Show answer
Sources
- McInnes, L., Healy, J. and Melville, J., "UMAP: Uniform Manifold Approximation and Projection for Dimension Reduction", arXiv:1802.03426. https://arxiv.org/abs/1802.03426
- "Nonlinear dimensionality reduction", Wikipedia. https://en.wikipedia.org/wiki/Nonlinear_dimensionality_reduction