Search

Search papers and lessons

UMAP: Uniform Manifold Approximation and Projection for Dimension Reduction

Leland McInnes, John Healy, James Melville · 2018

UMAP turns a high-dimensional dataset into a weighted nearest-neighbour graph, then lays out a 2D or 3D copy of that graph by gradient descent, matching t-SNE locally and running much faster.

On this page

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

TL;DR

UMAP is a method for dimension reduction, published by Leland McInnes, John Healy and James Melville in 2018. It takes a dataset with many features per point, such as a 784-pixel image, and places each point in two or three dimensions so the picture is useful to look at. It first builds a weighted graph that links each point to its nearest neighbours, then lays out a low-dimensional version of that graph by gradient descent. The paper reports that its pictures are competitive with t-SNE on local structure, keep more of the global layout, and take far less time to compute.

Builds on: K-Nearest Neighbors, Fuzzy Simplicial Set, Stochastic Gradient Descent, Graph Laplacian · Leads to: the use of UMAP as a general preprocessing step for machine learning, which the paper names as its goal

Why it mattered

Before UMAP, the standard tool for a good two-dimensional picture of complex data was t-SNE. The paper's own introduction says that, based on preliminary software releases, UMAP "has already found widespread use" in bioinformatics, materials science and machine learning. The paper's own experiments include a mouse single-cell gene expression dataset, one of the biology uses it cites. The paper also argues that UMAP is more than a picture tool: it works for output dimensions above 2, so it can serve as a preprocessing step before another model.

History

Dimension reduction has a long history. Principal component analysis, multidimensional scaling and Sammon mapping were the early tools, and they preserve distances across the whole dataset. The 2000s brought manifold methods such as Isomap and Laplacian eigenmaps, which preserve only local distances, and t-SNE, which the paper calls the current state of the art for visualization. The paper places UMAP in the local-distance family and cites the earlier Laplacian eigenmaps work of Belkin and Niyogi as its mathematical starting point, adding a construction from Spivak's work on fuzzy simplicial sets.

The problem

Suppose each of N points has n features, and n is large. Nearby points in that high-dimensional space are usually similar, but the distances in the full space are hard to read directly. Dimension reduction asks for points in d dimensions, with d equal to 2 or 3 for a plot, such that the picture keeps the structure that matters.

The paper names two families. The first keeps all pairwise distances as well as it can, which is the goal of PCA and multidimensional scaling. The second keeps local distances and tolerates stretching at large scales, which is the goal of t-SNE and Laplacian eigenmaps. Both have costs. A method that keeps all distances can miss the curved shape of data that lies on a manifold, a smooth surface inside the larger space. A method that keeps only local distances can be slow, can stretch the global layout, and in t-SNE's case scales poorly to very large datasets. UMAP aims at local structure with a theory that also says how far to trust it at larger scales.

How it works

Take a tiny example with k = 3 nearest neighbours. The algorithm has two phases. The first builds a weighted graph from the data. The second lays out a low-dimensional graph whose edges should look like the first.

  1. Find each point's nearest neighbours. For point x_i, find its k nearest neighbours. Suppose point x_i has neighbours at distances 1, 2 and 4. Check: these are the three closest points, in order.
  2. Measure distance from the nearest neighbour. Let ρ_i be the smallest distance above zero, here 1. This ensures that x_i has at least one neighbour with edge weight exactly 1, so the graph is connected locally.
  3. Pick a local scale. Choose σ_i so that the weights of the neighbours add up to log₂(k). With k = 3 the target is about 1.585. Solving for this example gives σ_i ≈ 1.35. Check: the weights in the next step should sum to 1.585.
  4. Compute edge weights. Each edge weight is exp(−max(0, d − ρ_i) / σ_i). Here the three weights are 1.00, 0.48 and 0.11. Check: they sum to 1.59, matching step 3 up to rounding.
  5. Make the graph symmetric. The weight from x_i to x_j may differ from the weight back. If A_ij = 0.8 and A_ji = 0.5, the combined weight is 0.8 + 0.5 − 0.8·0.5 = 0.9. Check: the result is at least as large as either direction, which is the "at least one edge exists" rule.
  6. Lay out a low-dimensional graph. Start from a spectral layout computed from the graph Laplacian. Then pull connected points together and push random pairs apart, using the attractive and repulsive forces in the paper's Section 3.2. Check: a point with a weight-1 edge to a neighbour is pulled toward it; a random far point is pushed away.
flowchart LR
  A[Data points] --> B[k nearest neighbours]
  B --> C[Local scale rho and sigma]
  C --> D[Weighted directed graph]
  D --> E[Symmetrize with a union]
  E --> F[Spectral initial layout]
  F --> G[Gradient descent on cross-entropy]
  G --> H[2D or 3D embedding]
00.511.522.533.5400.20.40.60.81distance between two points in the layoutmembership strength
  • low-dimensional membership, a = 1, b = 1
  • target membership, min-dist = 0
The layout is rewarded when its membership curve matches the target. Here a = b = 1 is only an illustration; the paper fits a and b to a chosen min-dist.

The math

The paper treats each point's neighbourhood as a fuzzy set of edges. An edge (x_i, x_j) has a membership strength μ in [0, 1], which is read as the probability that the edge exists. Two graphs, one from the data and one from the layout, are compared by the cross-entropy between these probabilities.

Step 2 and 3 in symbols: the local scale

Let d(x_i, x_{i_j}) be the distance from x_i to its j-th nearest neighbour, for j = 1 to k. Define

ρ_i = min { d(x_i, x_{i_j}) : d(x_i, x_{i_j}) > 0 }

and choose σ_i as the solution of

Σ_j exp( −max(0, d(x_i, x_{i_j}) − ρ_i) / σ_i ) = log₂(k).

The left side is a sum of k weights; the right side is a fixed target. The value σ_i is found by binary search, since the left side falls as σ_i grows. σ_i plays the role of a local ruler: a point in a dense region gets a small σ_i, so its neighbours are not all treated as close.

Step 4 in symbols: the edge weight

w(x_i, x_j) = exp( −max(0, d(x_i, x_j) − ρ_i) / σ_i ).

The weight is 1 at the nearest neighbour, because d − ρ_i = 0 there. It decays with distance beyond that.

Step 5 in symbols: the union

With A the matrix of directed weights, the symmetric graph is

B = A + Aᵀ − A ∘ Aᵀ,

where ∘ is the entry-by-entry product. This is the probabilistic "or": if the two directed edges exist independently with probabilities a and b, the probability that at least one exists is a + b − ab.

Step 6 in symbols: the layout objective

For points y_i in the low-dimensional space, define the layout membership

Φ(y_i, y_j) = 1 / (1 + a · ‖y_i − y_j‖^{2b}),

where a and b are fitted so that Φ matches the target Ψ, which is 1 when the distance is at most min-dist and exp(−(distance − min-dist)) otherwise. The objective is the cross-entropy

−Σ [ B_ij log Φ_ij + (1 − B_ij) log(1 − Φ_ij) ].

The first term pulls connected points together. The second pushes unconnected points apart. The paper does not sum over all pairs for the second term; it samples a few random negative pairs per update, optimised by stochastic gradient descent, and that is the source of the speed.

Worked examples

Example
1. Tiny: the local scale for one point
  1. Point x_i has k = 3 neighbours at distances 0.5, 1.5 and 2.5.
  2. Here ρ_i = 0.5, the smallest positive distance.
  3. The target sum is log₂(3) ≈ 1.585.
  4. Let s = exp(−1/σ). The three terms are 1, s (distance 1.5 minus 0.5 is 1) and s² (distance 2.5 minus 0.5 is 2, so exp(−2/σ) = s²). The equation becomes 1 + s + s² = 1.585, so s² + s − 0.585 = 0.
  5. Solving the quadratic gives s ≈ 0.414, so σ = −1/ln(0.414) ≈ 1.13.
  6. Check: the weights are 1.00, exp(−1/1.13) ≈ 0.41 and exp(−2/1.13) ≈ 0.17. They sum to about 1.58, matching the target.
  7. Interpretation: the spacing of the neighbours sets the local ruler. Here the second and third neighbours are only a little weaker than the first, so σ is moderate.
Example
2. Standard: a symmetric edge
  1. Directed weights: A_ij = 0.6 and A_ji = 0.3.
  2. Symmetric weight: B_ij = 0.6 + 0.3 − 0.6·0.3 = 0.9 − 0.18 = 0.72.
  3. Check: 0.72 is larger than both 0.6 and 0.3, as the union should be.
  4. Suppose the layout gives Φ_ij = 0.5. The attractive term for this pair is −B_ij log Φ_ij = −0.72·log 0.5, which is −0.72 · (−0.693) ≈ 0.499.
  5. The repulsive term for the same pair is −(1 − B_ij) log(1 − Φ_ij) = −0.28 · log 0.5 ≈ 0.194.
  6. Total for this pair: 0.499 + 0.194 ≈ 0.693, which is ln 2, because 0.72 + 0.28 = 1 and Φ = 0.5 makes each term −ln 0.5.
  7. Interpretation: when the layout is uncertain (Φ = 0.5), the loss is ln 2 whatever B is. Moving Φ toward B lowers the loss, which is what the gradient step does.
Example
3. Realistic: two handwritten digits in a 784-feature image set

Take MNIST, where each point is a 28 by 28 image, so n = 784. Suppose point x_i is a "4" and its nearest neighbour x_j is another "4" at distance 3.0. Its nearest distance is ρ_i = 2.0, and suppose σ_i = 1.5.

  1. Edge weight from x_i to x_j: exp(−max(0, 3.0 − 2.0)/1.5) = exp(−1/1.5) = exp(−0.667) ≈ 0.51.
  2. Suppose the reverse weight is A_ji = 0.40.
  3. Symmetric weight: B = 0.51 + 0.40 − 0.51·0.40 = 0.91 − 0.204 = 0.706.
  4. The layout puts the two "4"s at distance 0.5 apart, and with a = b = 1 the layout membership is Φ = 1 / (1 + 0.25) = 0.8.
  5. Attractive term: −0.706 · ln 0.8 = −0.706 · (−0.223) ≈ 0.158.
  6. Repulsive term: −0.294 · ln 0.2 = −0.294 · (−1.609) ≈ 0.473.
  7. The layout membership Φ = 0.8 is higher than the target B ≈ 0.71, so the gradient step lowers Φ by pushing the two points slightly apart. Interpretation: the layout is asked to keep neighbours close but not collapse them into one spot.

Try it: an edge has A_ij = 0.5 and A_ji = 0.5. What is the symmetric weight B? Answer: 0.5 + 0.5 − 0.25 = 0.75.

Common mistakes and tradeoffs

It is tempting to think UMAP is a faster t-SNE with the same meaning. In fact the two optimize different things: t-SNE matches neighbour probabilities with a Kullback-Leibler divergence, while UMAP matches graph edges with a cross-entropy, and the paper argues that the graph view is what makes UMAP's theory fit the data.

It is tempting to think the distances on a UMAP plot are meaningful. In fact the paper's emphasis is on local structure: clusters and their rough arrangement are what the plot is designed to show. Distances between far apart clusters and cluster sizes are not guaranteed to mean what they seem to mean, because the layout is only an approximation of the graph.

It is tempting to think a bigger n (neighbours) is always better. In fact the paper says small n keeps fine structure but may split the data into many pieces, while large n shows broad structure and blurs detail. The min-dist parameter mainly changes how tightly points pack in the picture, and the paper calls it "essentially aesthetic".

When to use it: visualizing large datasets, and as a preprocessing step when the output dimension must be larger than 3. When not to use it: when exact distances between far apart points must be preserved, or when a linear method such as PCA is enough. The paper's own Section 6 lists weaknesses, which this lesson does not reproduce in detail.

Results

The paper reports k-nearest-neighbour classifier accuracy on embeddings, which is a proxy for how well the embedding keeps class structure, and run times on a range of datasets. Some runtime benchmarks used subsampled data or a PCA reduction first, so those timings are not on the full raw data.

  • PenDigits and COIL-20 (Table 1, k-nearest-neighbour classifier, 10-fold). On COIL-20 at k = 160, UMAP scores 0.803 against 0.609 for t-SNE and 0.616 for LargeVis. At k = 10 on PenDigits, t-SNE scores 0.977 and UMAP 0.973, so the two are close at small k.
  • Shuttle, MNIST and Fashion-MNIST (Table 2). On Shuttle at k = 3200, UMAP scores 0.957 against 0.828 for t-SNE. On MNIST at k = 3200, UMAP scores 0.964 against 0.946 for t-SNE. On Fashion-MNIST, t-SNE is ahead at small k (0.818 against 0.790 at k = 100) and the two are close at large k.
  • Run time (Table 3). On COIL-100, UMAP takes 85 seconds, against 810 seconds for t-SNE and 3,197 seconds for LargeVis. On MNIST, UMAP takes 87 seconds, against 1,450 seconds for t-SNE. On the 200,000-point GoogleNews sample, UMAP takes 361 seconds, against 16,906 seconds for t-SNE. On PenDigits, the smallest dataset, Laplacian eigenmaps and Isomap are faster.
  • Stability (Section 5.3, Figure 8). The paper reports that UMAP embeddings stay very close to the full embedding even when the data is subsampled to 5 percent, and that this is more stable than t-SNE, measured by Procrustes distance. The detailed numbers are in the figure, not the text.

The limits are the paper's own. The kNN accuracy measure rewards clusters that a classifier can separate, and the authors say it can be argued that UMAP keeps more global structure than t-SNE, which is a judgment from the figures rather than a number. The theory is built on an assumption of uniformly distributed data on a manifold, which the paper notes finite real-world data is rarely like.

Reading guide

The paper is long, with a theory section that a first-time reader can skip. Read it in this order.

Check yourself

QuestionRecall: what does UMAP build in its first phase, and what does it optimize in its second?Show answer

The first phase builds a weighted graph in which each point is linked to its nearest neighbours, with weights that decay with distance beyond the nearest neighbour. The second phase lays out a low-dimensional graph and optimizes it by gradient descent so that its edges match the weights of the first graph, as measured by cross-entropy.

QuestionComputation: a point has directed weights `A_ij = 0.4` and `A_ji = 0.8`. What is the symmetric weight `B_ij`?Show answer

B_ij = A_ij + A_ji − A_ij · A_ji = 0.4 + 0.8 − 0.32 = 0.88. Check: the result is larger than both inputs.

QuestionWhy: the paper says the nearest-neighbour distance `ρ_i` is subtracted from every distance. What would go wrong if it were not?Show answer

Without the subtraction, the nearest neighbour would have a weight below 1, so no point would be guaranteed an edge of weight 1. The graph might then fail to be locally connected, which the paper treats as a requirement for real data. The subtraction is what makes each point connect to at least one neighbour with full strength.

QuestionApplied: a biologist plots 50,000 cells in two dimensions. Two clusters look close together on the picture. Can she conclude they are similar?Show answer

Not from distance alone. The paper says UMAP's layout is meant to show local structure and the rough arrangement of clusters, and the distance between clusters on the plot is not guaranteed to match their true similarity. She should check the clusters with a separate measure, such as the genes that distinguish them, and try a few values of the neighbour count and min-dist to see whether the arrangement holds.

Sources

Read the full paper

Leland McInnes, John Healy, James Melville, 2018. arXiv:1802.03426