UMAP (Uniform Manifold Approximation and Projection) › Formula
Machine Learning from ScratchAll chapters 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 in our dataset, we measure how far it is from other points using Euclidean distance:
where is the distance between points and , and 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 , we identify its nearest neighbors. These neighbors define a local patch of the manifold around . The parameter (called n_neighbors in practice) controls how many neighbors we consider. A smaller focuses on very local structure, while a larger 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 belongs to point 's local neighborhood.
The membership strength is computed as:
where:
- : Membership strength indicating how strongly point belongs to the neighborhood of point
- : Distance from to its nearest neighbor (acts as a baseline)
- : Scaling factor specific to point that controls decay rate
- : Euclidean distance between points and
Any point closer than the nearest neighbor (when ) gets maximum membership strength of 1.0, since and . Beyond the nearest neighbor, the membership strength decays exponentially at a rate controlled by .
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
The parameter is not chosen arbitrarily. For each point , we pick so that the effective number of neighbors equals . Mathematically, we want:
where:
- : Sum of all membership strengths for point (excluding itself)
- : Target sum based on the number of neighbors
Why instead of just ? This comes from information theory and ensures that the fuzzy simplicial set has the right "size" in terms of entropy. We find using binary search: we try different values until the sum of membership strengths equals .
This adaptive scaling accounts for local density. In dense regions of the data, will be small (neighbors are close, so we don't need to scale distances much). In sparse regions, will be large (neighbors are farther away, so we scale up to maintain connectivity).
Step 3: Creating a Global Graph
Each point now has its own local view of the neighborhood structure, encoded by the membership strengths . But these local views are asymmetric: point might consider a strong neighbor while considers a weak neighbor (or not at all).
To create a consistent global structure, we symmetrize these relationships using a fuzzy set union:
where:
- : Symmetrized global membership strength between points and
- : Local membership strength from point 's perspective
- : Local membership strength from point 's perspective
This formula is the probabilistic OR operation for fuzzy sets. It is equivalent to:
In practice, UMAP often uses a simpler approximation:
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 that describes relationships in high dimensions. We need to find low-dimensional coordinates 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:
where:
- : Membership strength between points and in the low-dimensional embedding
- : Low-dimensional coordinates of points and
- : 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 ).
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 achieves this balance, where 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 and are set to match this desired minimum distance.
Step 5: Optimization via Cross-Entropy
We now have two sets of membership strengths: (the target from high dimensions) and (the current state in low dimensions). We want to adjust the low-dimensional coordinates to make match as closely as possible.
UMAP uses cross-entropy as the objective function:
where:
- : Cross-entropy loss to be minimized
- : Target membership strength from high-dimensional space
- : Current membership strength in low-dimensional space
Cross-entropy measures the difference between two probability distributions. Here we treat and as probabilities that points and are connected.
The first term, , penalizes cases where points should be close (high ) but are far apart (low ). The second term, , 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 . 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:
-
Local structure preservation: By using nearest neighbors and adaptive scaling (), UMAP ensures that local neighborhoods are well-represented.
-
Global structure preservation: By connecting local views into a global fuzzy simplicial set, UMAP maintains large-scale relationships.
-
Flexibility: The low-dimensional membership function and
min_distparameter let us control the final embedding's appearance. -
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_neighborsparameter controls the size of local neighborhoods (larger values capture more global structure). Themin_distparameter 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.