Search

Search papers and lessons

UMAP

Level 3 · 12 min read

UMAP maps high-dimensional points to two or three dimensions by matching a fuzzy nearest-neighbour graph of the data, so points that were close stay close in the picture.

On this page

  • Colour = topic mix
  • Linear algebra
  • Discrete math
  • Probability
  • Calculus
  • Learning basics
  • Losses & regularization
  • ReadReady to readNot yet

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 k=2k = 2 neighbours per point, found as in K-Nearest Neighbors, and use a fixed σ=1\sigma = 1 so the arithmetic stays simple. The real method chooses σ\sigma per point, as the math section explains.

  1. 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.
  2. Give each point a local scale. Let ρi\rho_i be the distance from point ii to its nearest neighbour. So ρA=1\rho_A = 1, ρB=1\rho_B = 1, ρC=1\rho_C = 1, ρD=8\rho_D = 8. The nearest neighbour always gets weight 1, which guarantees that every point is connected to something.
  3. Turn distances into weights. For a neighbour jj of ii, the weight is exp⁡(−(dij−ρi)/σi)\exp(-(d_{ij} - \rho_i) / \sigma_i), where dijd_{ij} is the distance and σi\sigma_i is a per-point scale. Take σ=1\sigma = 1 for this illustration. The weight of D to C is exp⁡(0)=1\exp(0) = 1. Check: d=8=ρDd = 8 = \rho_D, so the exponent is zero.
  4. Make the graph symmetric. A may count B as a neighbour while B does not count A. The union rule combines the two directions: v=vone way+vother way−vone way⋅vother wayv = v_{\text{one way}} + v_{\text{other way}} - v_{\text{one way}} \cdot v_{\text{other way}}. For A and B, both directions are 1, so v=1+1−1=1v = 1 + 1 - 1 = 1. Check: a weight of 1 in either direction gives 1 overall.
  5. Lay out the points so the picture has the same weights. Each pair in the picture gets a target closeness ww that falls with distance. We place points in two dimensions so that the closeness of each pair matches its weight vv. A pair with v=1v = 1 should be almost touching, and a pair with a small vv should be far apart.
  6. 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

Definition: Fuzzy neighbour weight
For points xix_i and a neighbour xjx_j in the kk nearest neighbours of xix_i, with distance dijd_{ij}, let ρi\rho_i be the distance to the nearest neighbour and let σi>0\sigma_i > 0 be a per-point scale. The directed weight is vj∣i=exp⁡(−max⁡(0, dij−ρi)/σi)v_{j \mid i} = \exp\bigl(-\max(0,\, d_{ij} - \rho_i) / \sigma_i\bigr), a number between 0 and 1. Here max⁡(0,⋅)\max(0, \cdot) means the value is clipped at zero, so the nearest neighbour always has weight 1.

The symmetric weight combines the two directions, using the rule for the union of two fuzzy sets in Fuzzy Set:

vij=vj∣i+vi∣j−vj∣i vi∣jv_{ij} = v_{j \mid i} + v_{i \mid j} - v_{j \mid i}\, v_{i \mid j}

The scale σi\sigma_i is not chosen by hand. It is the value that makes the weights of point ii add up to log⁡2k\log_2 k, which keeps the total "amount of neighbourhood" the same for every point regardless of density. Here log⁡2k\log_2 k is the base-2 logarithm of kk.

Step 1 in symbols

The neighbour lists are the kk smallest values of dijd_{ij} for each ii. In the walkthrough, ρA=dAB=1\rho_A = d_{AB} = 1, and ρD=dDC=8\rho_D = d_{DC} = 8.

Steps 2 and 3 in symbols

With σ=1\sigma = 1 and ρD=8\rho_D = 8, the weight of D to C is vC∣D=exp⁡(−max⁡(0,8−8)/1)=e0=1v_{C \mid D} = \exp(-\max(0, 8 - 8)/1) = e^0 = 1. The weight of D to B, at distance 9, is exp⁡(−(9−8))=e−1≈0.368\exp(-(9 - 8)) = e^{-1} \approx 0.368.

Step 4 in symbols

For the pair A and C, each list contains the other, with vC∣A=vA∣C=e−1≈0.368v_{C \mid A} = v_{A \mid C} = e^{-1} \approx 0.368. The formula gives 0.368+0.368−0.3682≈0.6000.368 + 0.368 - 0.368^2 \approx 0.600, the weight 0.6 used in step 6. If only one list contained the other, the weight would be just 0.3680.368.

Step 5 in symbols

The low-dimensional closeness of two points yiy_i and yjy_j in the picture is

wij=11+a ∥yi−yj∥2bw_{ij} = \frac{1}{1 + a\,\lVert y_i - y_j \rVert^{2b}}

where aa and bb 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 wijw_{ij} close to vijv_{ij}, by minimising the cross-entropy between the two sets of weights:

C=∑i≠j[vijlog⁡vijwij+(1−vij)log⁡1−vij1−wij]\mathcal{C} = \sum_{i \ne j} \Bigl[ v_{ij} \log \frac{v_{ij}}{w_{ij}} + (1 - v_{ij}) \log \frac{1 - v_{ij}}{1 - w_{ij}} \Bigr]

The first term pulls pairs with high vv together. The second pushes apart pairs with low vv. 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

Example
1. One weight, by hand (tiny)
  1. Point xx has nearest neighbour at distance d=1d = 1, so ρ=1\rho = 1.
  2. Another neighbour yy is at distance d=3d = 3. Take σ=1\sigma = 1.
  3. Weight of yy from xx: v=exp⁡(−max⁡(0,3−1)/1)=exp⁡(−2)v = \exp(-\max(0, 3 - 1) / 1) = \exp(-2).
  4. Compute: e−2≈0.135e^{-2} \approx 0.135.
  5. Interpretation: yy is two units farther than the nearest neighbour, so its weight drops from 1 to about 0.135.

Try it: a third point zz is at distance 2 from xx, with ρ=1\rho = 1 and σ=1\sigma = 1. What is its weight, and is it bigger or smaller than the weight of yy? Answer: exp⁡(−(2−1))=e−1≈0.368\exp(-(2 - 1)) = e^{-1} \approx 0.368. It is bigger than the weight 0.135 of yy, because zz is closer.

Example
2. Checking a layout with the loss (standard)

Suppose a pair has symmetric weight v=0.6v = 0.6. We compare three candidate closenesses ww for that pair in the picture, using one term of the cross-entropy, −[vlog⁡w+(1−v)log⁡(1−w)]-[v \log w + (1 - v) \log(1 - w)].

  1. For w=0.6w = 0.6: −[0.6ln⁡0.6+0.4ln⁡0.4]=−[0.6(−0.511)+0.4(−0.916)]=0.306+0.367=0.673-[0.6 \ln 0.6 + 0.4 \ln 0.4] = -[0.6(-0.511) + 0.4(-0.916)] = 0.306 + 0.367 = 0.673.
  2. For w=0.9w = 0.9: −[0.6ln⁡0.9+0.4ln⁡0.1]=−[0.6(−0.105)+0.4(−2.303)]=0.063+0.921=0.984-[0.6 \ln 0.9 + 0.4 \ln 0.1] = -[0.6(-0.105) + 0.4(-2.303)] = 0.063 + 0.921 = 0.984.
  3. For w=0.3w = 0.3: −[0.6ln⁡0.3+0.4ln⁡0.7]=−[0.6(−1.204)+0.4(−0.357)]=0.722+0.143=0.865-[0.6 \ln 0.3 + 0.4 \ln 0.7] = -[0.6(-1.204) + 0.4(-0.357)] = 0.722 + 0.143 = 0.865.
  4. Compare: 0.673 is the smallest, and it occurs at w=v=0.6w = v = 0.6.
  5. Convert to a distance with a=b=1a = b = 1, a simplified choice for hand calculation: 1/(1+d2)=0.61 / (1 + d^2) = 0.6 gives d2=1/0.6−1=0.667d^2 = 1/0.6 - 1 = 0.667, so d≈0.82d \approx 0.82.
  6. Interpretation: the layout should place this pair about 0.82 units apart. A pair that should be closer, with v=1v = 1, would need d=0d = 0, and in practice min_dist sets how tightly the points may pack.

Try it: a pair with v=0.2v = 0.2 is placed so that w=0.2w = 0.2. Using a=b=1a = b = 1, what distance should separate them? Answer: 1/(1+d2)=0.21/(1 + d^2) = 0.2 gives d2=4d^2 = 4, so d=2d = 2. A weak neighbour belongs about two and a half times as far away as the 0.82 of the previous step.

Example
3. Picking a scale for a realistic point (realistic)

Take a point whose k=3k = 3 nearest neighbours are at distances 2.0, 2.5 and 3.0 in a high-dimensional space. The nearest distance is ρ=2.0\rho = 2.0. The target total weight is log⁡23≈1.585\log_2 3 \approx 1.585. The weights are 11 for the nearest neighbour, exp⁡(−0.5/σ)\exp(-0.5/\sigma) for the one at 2.5, and exp⁡(−1.0/σ)\exp(-1.0/\sigma) for the one at 3.0. We need the scale σ\sigma that makes the sum match.

  1. Equation to solve: 1+e−0.5/σ+e−1.0/σ=1.5851 + e^{-0.5/\sigma} + e^{-1.0/\sigma} = 1.585, so e−0.5/σ+e−1.0/σ=0.585e^{-0.5/\sigma} + e^{-1.0/\sigma} = 0.585.
  2. Try σ=0.5\sigma = 0.5: e−1+e−2=0.368+0.135=0.503e^{-1} + e^{-2} = 0.368 + 0.135 = 0.503. This is too small.
  3. Try σ=0.6\sigma = 0.6: e−0.833+e−1.667=0.435+0.189=0.624e^{-0.833} + e^{-1.667} = 0.435 + 0.189 = 0.624. This is too big.
  4. Try σ=0.567\sigma = 0.567: e−0.882+e−1.764=0.414+0.172=0.586e^{-0.882} + e^{-1.764} = 0.414 + 0.172 = 0.586. This is close enough to 0.585.
  5. So the point's scale is about σ≈0.57\sigma \approx 0.57.
  6. 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 σ=0.5\sigma = 0.5, what is the weight of the neighbour at distance 3.0? Answer: exp⁡(−(3.0−2.0)/0.5)=e−2≈0.135\exp(-(3.0 - 2.0)/0.5) = e^{-2} \approx 0.135. 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
It gets weight 1. The weight is exp⁡(−max⁡(0,d−ρ)/σ)\exp(-\max(0, d - \rho)/\sigma), and for the nearest neighbour d=ρd = \rho, so the exponent is zero. This guarantees every point is connected to at least one neighbour.
QuestionComputation: a neighbour is at distance 4.0, the nearest distance is ρ=1.0\rho = 1.0, and σ=1.5\sigma = 1.5. What is its directed weight?Show answer
The exponent is −(4.0−1.0)/1.5=−2-(4.0 - 1.0)/1.5 = -2, so the weight is e−2≈0.135e^{-2} \approx 0.135.
QuestionWhy: why does UMAP use a per-point scale instead of one global distance threshold?Show answer
Because density differs across the data. A single threshold would connect every point in a dense region and leave sparse points isolated, or the reverse. A per-point scale, set so each point's weights add up to a fixed amount, gives comparable neighbourhoods in both kinds of region.
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
No. Distances in the picture are not measurements. UMAP matches neighbour weights, so the gap between two groups shows that they have few neighbour links, not how far apart they are. To compare how different groups are, measure in the original data, for example with the nearest-neighbour graph itself.

Sources