UMAP (Uniform Manifold Approximation and Projection) › Formula

Michael BrenndoerferJuly 24, 20258 min read
Machine Learning from ScratchAll chapters
Part 6 of 9Explainability
Chapter 50UMAP (Uniform Manifold Approximation and Projection) › Formula
Reading section 2 of 8 · Formula · 8 minSection 2 of 8 · 8 min

Book contents

Section 2 of 8 · 8 min read

UMAP (Uniform Manifold Approximation and Projection) › FormulaOne section · Sections in this chapter2 / 8 ↓
Reading tools Definitions & inline links

Choose your expertise level to adjust how many terms are explained. Beginners see more tooltips, experts see fewer to maintain reading flow. Hover over underlined terms for instant definitions.

Article links

Make inline references clickable

To understand how UMAP achieves its goal of preserving both local and global structure, we need to explore the mathematical machinery that powers it. The algorithm builds on two key ideas from topology: fuzzy simplicial sets (a way to represent how points relate to each other with partial membership) and Riemannian geometry (which lets us think about distances on curved surfaces). Let's walk through the construction step by step, focusing on the intuition behind each piece.

The Core Challenge

Before diving into formulas, consider the problem UMAP solves. We have high-dimensional data and want to create a low-dimensional representation that preserves important relationships. Linear methods like PCA assume the data lies on a flat hyperplane, but real data often lives on curved manifolds. Imagine a Swiss roll: the data lies on a 2D surface embedded in 3D space. If we simply project it onto a plane, we lose the intrinsic structure. UMAP addresses this by modeling the data as lying on a manifold and preserving the manifold's topology.

The algorithm proceeds in two phases: first, build a rich representation of the high-dimensional structure; second, find a low-dimensional arrangement that matches this structure as closely as possible.

Phase 1: Representing High-Dimensional Structure

Step 1: Computing Local Distances

We start with the basics. For each point xix_i in our dataset, we measure how far it is from other points using Euclidean distance:

dij=∥xi−xj∥2d_{ij} = \|x_i - x_j\|_2

where dijd_{ij} is the distance between points xix_i and xjx_j, and ∥⋅∥2\|\cdot\|_2 denotes the Euclidean (L2) norm.

This gives us a distance matrix. UMAP focuses on local neighborhoods because nearby points provide more reliable information about the manifold structure than distant ones.

For each point xix_i, we identify its kk nearest neighbors. These neighbors define a local patch of the manifold around xix_i. The parameter kk (called n_neighbors in practice) controls how many neighbors we consider. A smaller kk focuses on very local structure, while a larger kk captures more global patterns.

Step 2: Building Fuzzy Membership Strengths

Instead of using a binary neighborhood (where points are either neighbors or not), UMAP assigns a smooth membership strength to each potential neighbor. This membership strength tells us how strongly point xjx_j belongs to point xix_i's local neighborhood.

The membership strength is computed as:

μij=exp⁡(−max⁡(0,dij−ρi)σi)\mu_{ij} = \exp\left(-\frac{\max(0, d_{ij} - \rho_i)}{\sigma_i}\right)

where:

  • μij\mu_{ij}: Membership strength indicating how strongly point xjx_j belongs to the neighborhood of point xix_i
  • ρi\rho_i: Distance from xix_i to its nearest neighbor (acts as a baseline)
  • σi\sigma_i: Scaling factor specific to point xix_i that controls decay rate
  • dijd_{ij}: Euclidean distance between points xix_i and xjx_j

Any point closer than the nearest neighbor (when dij≤ρid_{ij} \leq \rho_i) gets maximum membership strength of 1.0, since max⁡(0,dij−ρi)=0\max(0, d_{ij} - \rho_i) = 0 and exp⁡(0)=1\exp(0) = 1. Beyond the nearest neighbor, the membership strength decays exponentially at a rate controlled by σi\sigma_i.

Why use this exponential decay? Because it creates a smooth, continuous transition from "definitely a neighbor" to "definitely not a neighbor." This smoothness is important for the optimization that comes later.

Finding the Right Scale with σi\sigma_i

The parameter σi\sigma_i is not chosen arbitrarily. For each point xix_i, we pick σi\sigma_i so that the effective number of neighbors equals kk. Mathematically, we want:

∑j≠iμij=log⁡2(k)\sum_{j \neq i} \mu_{ij} = \log_2(k)

where:

  • ∑j≠iμij\sum_{j \neq i} \mu_{ij}: Sum of all membership strengths for point xix_i (excluding itself)
  • log⁡2(k)\log_2(k): Target sum based on the number of neighbors kk

Why log⁡2(k)\log_2(k) instead of just kk? This comes from information theory and ensures that the fuzzy simplicial set has the right "size" in terms of entropy. We find σi\sigma_i using binary search: we try different values until the sum of membership strengths equals log⁡2(k)\log_2(k).

This adaptive scaling accounts for local density. In dense regions of the data, σi\sigma_i will be small (neighbors are close, so we don't need to scale distances much). In sparse regions, σi\sigma_i will be large (neighbors are farther away, so we scale up to maintain connectivity).

Step 3: Creating a Global Graph

Each point xix_i now has its own local view of the neighborhood structure, encoded by the membership strengths μij\mu_{ij}. But these local views are asymmetric: point xix_i might consider xjx_j a strong neighbor while xjx_j considers xix_i a weak neighbor (or not at all).

To create a consistent global structure, we symmetrize these relationships using a fuzzy set union:

μijglobal=μij+μji−μij⋅μji\mu_{ij}^{\text{global}} = \mu_{ij} + \mu_{ji} - \mu_{ij} \cdot \mu_{ji}

where:

  • μijglobal\mu_{ij}^{\text{global}}: Symmetrized global membership strength between points ii and jj
  • μij\mu_{ij}: Local membership strength from point ii's perspective
  • μji\mu_{ji}: Local membership strength from point jj's perspective

This formula is the probabilistic OR operation for fuzzy sets. It is equivalent to:

μijglobal=1−(1−μij)(1−μji)\mu_{ij}^{\text{global}} = 1 - (1 - \mu_{ij})(1 - \mu_{ji})

In practice, UMAP often uses a simpler approximation:

μijglobal≈max⁡(μij,μji)\mu_{ij}^{\text{global}} \approx \max(\mu_{ij}, \mu_{ji})

This approximation says: if either point considers the other a neighbor, preserve that connection in the global graph. The result is a weighted graph where edge weights represent the strength of relationships in the original high-dimensional space.

Phase 2: Finding the Low-Dimensional Embedding

Step 4: Defining Structure in Low Dimensions

Now we have a target: the fuzzy simplicial set μijglobal\mu_{ij}^{\text{global}} that describes relationships in high dimensions. We need to find low-dimensional coordinates yiy_i that recreate these relationships as faithfully as possible.

In the low-dimensional space, we compute similar membership strengths. But instead of using exponential decay, UMAP uses a different functional form:

vij=11+a⋅∥yi−yj∥22bv_{ij} = \frac{1}{1 + a \cdot \|y_i - y_j\|_2^{2b}}

where:

  • vijv_{ij}: Membership strength between points ii and jj in the low-dimensional embedding
  • yi,yjy_i, y_j: Low-dimensional coordinates of points ii and jj
  • a,ba, b: Shape parameters that control the distribution (set based on min_dist)

This function is a generalized version of the Cauchy distribution (which t-SNE uses with b=1b = 1).

Why a different function in low dimensions? The exponential decay in high dimensions reflects local connectivity on the manifold. In low dimensions, we need a function that allows both tight clusters (when points should be close) and clear separation (when points should be far apart). The form 11+a⋅d2b\frac{1}{1 + a \cdot d^{2b}} achieves this balance, where d=∥yi−yj∥2d = \|y_i - y_j\|_2 is the Euclidean distance in the low-dimensional space.

The Role of min_dist

The min_dist parameter specifies the minimum distance between points in the low-dimensional embedding. Smaller values allow points to pack tightly (good for revealing fine structure), while larger values spread points out (good for seeing overall organization). The parameters aa and bb are set to match this desired minimum distance.

Step 5: Optimization via Cross-Entropy

We now have two sets of membership strengths: μijglobal\mu_{ij}^{\text{global}} (the target from high dimensions) and vijv_{ij} (the current state in low dimensions). We want to adjust the low-dimensional coordinates yiy_i to make vijv_{ij} match μijglobal\mu_{ij}^{\text{global}} as closely as possible.

UMAP uses cross-entropy as the objective function:

C=∑i,jμijgloballog⁡(μijglobalvij)+(1−μijglobal)log⁡(1−μijglobal1−vij)C = \sum_{i,j} \mu_{ij}^{\text{global}} \log\left(\frac{\mu_{ij}^{\text{global}}}{v_{ij}}\right) + (1 - \mu_{ij}^{\text{global}}) \log\left(\frac{1 - \mu_{ij}^{\text{global}}}{1 - v_{ij}}\right)

where:

  • CC: Cross-entropy loss to be minimized
  • μijglobal\mu_{ij}^{\text{global}}: Target membership strength from high-dimensional space
  • vijv_{ij}: Current membership strength in low-dimensional space

Cross-entropy measures the difference between two probability distributions. Here we treat μijglobal\mu_{ij}^{\text{global}} and vijv_{ij} as probabilities that points ii and jj are connected.

The first term, μijgloballog⁡(μijglobal/vij)\mu_{ij}^{\text{global}} \log(\mu_{ij}^{\text{global}} / v_{ij}), penalizes cases where points should be close (high μijglobal\mu_{ij}^{\text{global}}) but are far apart (low vijv_{ij}). The second term, (1−μijglobal)log⁡((1−μijglobal)/(1−vij))(1 - \mu_{ij}^{\text{global}}) \log((1 - \mu_{ij}^{\text{global}}) / (1 - v_{ij})), penalizes cases where points should be far apart but are close together.

Minimizing this cross-entropy pulls the low-dimensional structure toward the high-dimensional structure. We use stochastic gradient descent (SGD) to optimize the coordinates yiy_i. At each iteration, we sample pairs of points, compute how the cross-entropy would change if we moved them slightly, and update coordinates to reduce the error.

Why This Construction Works

Let's step back and see the full picture:

  1. Local structure preservation: By using nearest neighbors and adaptive scaling (σi\sigma_i), UMAP ensures that local neighborhoods are well-represented.

  2. Global structure preservation: By connecting local views into a global fuzzy simplicial set, UMAP maintains large-scale relationships.

  3. Flexibility: The low-dimensional membership function and min_dist parameter let us control the final embedding's appearance.

  4. Mathematical foundation: The use of fuzzy sets and cross-entropy provides a principled objective function that can be efficiently optimized.

Mathematical Properties

The construction above gives UMAP several important properties:

  • Topological preservation: By building a fuzzy simplicial set, UMAP preserves topological features such as holes and loops, along with connected components. If two clusters are separate in high dimensions, they remain separate in low dimensions.

  • Metric structure preservation: UMAP adapts to the local density of the data. In dense regions, it preserves fine details. In sparse regions, it maintains connectivity without distorting distances too much.

  • Parameter interpretability: Each parameter has a clear geometric meaning. The n_neighbors parameter controls the size of local neighborhoods (larger values capture more global structure). The min_dist parameter controls how tightly points can cluster in the embedding (smaller values reveal finer details).

  • Computational efficiency: By focusing on local neighborhoods and using efficient optimization, UMAP scales to large datasets. The nearest neighbor search is the main computational bottleneck, but this can be accelerated with approximate methods.