2.5.12 · D3Unsupervised Learning

Worked examples — UMAP for dimensionality reduction

2,470 words11 min readBack to topic

This page is the case-by-case drill room for UMAP for dimensionality reduction. The parent note built the machinery (the high-D weight , the low-D weight , the calibration, the cross-entropy loss). Here we make sure no scenario surprises you: we enumerate every kind of input UMAP's formulas can meet, then work each one to a number, guessing first, checking at the end.

Everything below only uses symbols the parent already earned. As a lightning refresher, the two kernels are:


The scenario matrix

Think of every "kind of input" as a cell. A cell is degenerate if it makes a max, a division, or a log do something extreme. We must show all of them.

# Case class Concrete trigger Which formula is stressed Example
A Nearest neighbour itself () high-D → exactly Ex 1
B Duplicate / zero distance () clamp high-D → still Ex 1
C Ordinary far neighbour () positive exponent argument high-D decay Ex 2
D Bandwidth solve (find ) sum-to- constraint binary search on Ex 3
E Small vs large limit vs target moves Ex 4
F Symmetrization (directed → undirected) fuzzy union edge merge Ex 5
G Low-D, points coincident () denominator low-D Ex 6
H Low-D, points far apart () denominator low-D Ex 6
I Loss contribution per edge plug into cross-entropy attractive vs repulsive term Ex 7
J Real-world word problem scRNA-seq min_dist choice refit consequence Ex 8
K Exam twist "does UMAP output change if you rescale all ?" scale-invariance of Ex 9

The nine examples below hit every cell A–K.


Forecast: Guess both weights before reading. Can a weight ever exceed ?

  1. Compute the clamped gap for the nearest neighbour. . Why this step? The whole point of subtracting is that the closest neighbour lands exactly at the start of the decay. The max guarantees we never feed a negative number into the exponential.

  2. Exponentiate. . Why this step? is the ceiling of this kernel — a weight of means "fully connected". This is UMAP's local connectivity guarantee: every point has at least one neighbour it sees with certainty, so no point is ever orphaned.

  3. Now the duplicate (). Gap , so again . Why this step? If two samples are identical, their distance is smaller than . Without the clamp we'd get — an impossible "probability". The max(0,·) clamp is exactly what keeps a fuzzy membership inside .

Verify: Both weights equal , and is the maximum a probability can be. The clamp saved case B. ✓


Forecast: It's the 4th-closest neighbour in the parent's example. Bigger or smaller than ?

  1. Clamped gap: . Why this step? , so this neighbour lies past the nearest one — the decay is genuinely active here.

  2. Divide by bandwidth: . Why this step? sets the "reach". Dividing puts the gap in units of bandwidths: this point is bandwidths away.

  3. Exponentiate: . Why this step? The exponential turns "distance in bandwidths" into a smooth fade. bandwidths → membership , matching the parent's listed weight of .

Verify: Parent note lists weights — our (rounded) sits in slot 4. Units check: the exponent argument is (distance)/(distance) = dimensionless. ✓


Forecast: The nearest neighbour already contributes . Will the other two need to add only ?

  1. Fix the first term. (Example 1). So we need . Why this step? Isolating the guaranteed reduces the unknown to two decaying terms.

  2. Write the residual as a function of . Gaps are and : Why this step? is monotonically increasing in (bigger ⇒ slower decay ⇒ bigger weights). Monotonicity is why binary search works — there is exactly one root.

  3. Bracket & bisect. : ⇒ too small, go higher. : ⇒ too big, go lower. : ⇒ slightly low. : . Root . Why this step? Each halving keeps the root bracketed; ~20 iterations reach machine precision. We show enough to trap the answer between and .

Verify: At : . Adding the fixed gives . ✓ (Numeric check in VERIFY.)


Forecast: Does doubling double the target?

  1. : target . Since the nearest neighbour alone already contributes , the second neighbour's weight must sum to . Why this step? This forces tiny, so only the single closest point matters — maximally local, easily fragmented manifolds (matches parent's "small fragments").

  2. : target . Now far neighbours must jointly contribute , forcing large. Why this step? A large keeps distant points at nonzero weight — the graph reaches farther, capturing global context (parent's "large blends clusters").

  3. Growth rate. From to (a jump), the target grew , only . The deliberately dampens the demand. Why this step? Logarithmic growth means UMAP's "effective neighbourhood" scales gently — this is what keeps behaviour smooth as you sweep n_neighbors.

Verify: , , and — sub-linear, as claimed. ✓


Forecast: The undirected edge — closer to the small , the large , or above both?

  1. Apply the fuzzy union. . Why this step? This is the probabilistic OR: " links to or links to ". Two directed opinions merge into one undirected fact.

  2. Plug in. . Why this step? The subtraction removes the double-counted overlap, keeping the result .

  3. Sanity of magnitude. (both) — a union is at least as strong as its strongest member, never exceeding . Why this step? Confirms the OR-semantics: agreeing evidence reinforces, never cancels.

Verify: ✓, and if either weight were the union would be exactly (fully connected wins). This is the cross-entropy target used in Ex 7.


Forecast: At distance , is exactly ? At distance , near ?

  1. Coincident (case G), . , so . Why this step? This is why min_dist matters: with nothing stopping it, two points can pile up and score the maximum membership . min_dist re-fits to forbid this and enforce spacing.

  2. Far apart (case H), . . Since , this is . Then . Why this step? The heavy polynomial tail ( instead of ) decays slowly, so far points keep a tiny but nonzero membership — this is the anti-crowding tail the parent mentioned.

  3. Limit check. As , denominator , so ; as , . The kernel spans the full . Why this step? A valid fuzzy membership must live in at both extremes. ✓

Verify: , . Monotone decreasing between. ✓ See figure.

Figure — UMAP for dimensionality reduction

Forecast: How much more expensive is placing two truly-connected points far apart?

The per-edge loss is

  1. Good layout, . . Why this step? When , both logs are near — the loss nearly vanishes. Agreement is cheap.

  2. Bad layout, . . Why this step? A strongly-connected pair () placed almost apart () fires the first term hard — this is the attractive force pulling them together in SGD.

  3. Compare. — roughly costlier. Why this step? The gradient of w.r.t. is what SGD follows; a huge loss ⇒ a strong pull. This is literally how UMAP learns the layout.

Verify: Good , bad , ratio . Both non-negative (cross-entropy is ). ✓


Forecast: Which setting lets points pack tightly enough to reveal a small dense cluster?

  1. min_dist = 0.0. The fit lets as (exactly Ex 6a). Points may sit on top of each other. Why this step? Rare cell types are dense — a handful of near-identical expression profiles. Allowing tight packing keeps that density visible as a compact blob.

  2. min_dist = 0.8. The kernel is refit so stays low until ; short-range attraction is replaced by repulsion. Why this step? This "inflates" every cluster for readability but erases local density — the rare tight blob spreads out and can be missed.

  3. Decision. For discovering tight rare clusters, choose min_dist = 0.0. Why this step? The scientific goal (density-preservation) directly dictates the hyperparameter, exactly as the parent's Example 2 concluded.

Verify: Consistent with parent note: min_dist=0.0 → dense clusters, preserves density; min_dist=0.8 → inflated, loses density. ✓ (No new number.)


Forecast: Intuition says "distances changed, so yes." Check the algebra.

  1. Scale too. If all , then the nearest distance automatically (it's just one of the 's). Why this step? isn't a free parameter — it is a distance, so it rescales with everything else.

  2. The bandwidth solve rescales. The constraint is identical to the original if we let , because . Why this step? The binary search finds a new that is times the old one, leaving every exponent argument unchanged.

  3. Weights are untouched. Each is identical. So the fuzzy graph, the symmetrization, and the loss are all unchanged ⇒ the embedding is invariant to global rescaling. Why this step? This is the deep reason UMAP cares about relative neighbourhood geometry, not absolute units — cousin to why PCA centring matters but a global scale on all axes together does not change directions.

Verify: With : original Ex 3 had gaps and . Scaled gaps with give and — the same exponent arguments ⇒ same weights . ✓ (Checked in VERIFY.)


Recall Quick self-test

What is when is 's nearest neighbour? ::: Exactly , because and . Why the max(0,·) clamp? ::: A duplicate point () would give an exponent and a weight — impossible for a membership. The clamp caps it at . Solving uses which numerical method and why does it work? ::: Binary search — the weight-sum is monotone increasing in , so there is a unique root. As two low-D points coincide, ? ::: (denominator ). As they separate, . Does scaling all distances by change the embedding? ::: No — absorbs the factor, leaving every unchanged.

Related tools worth contrasting after this drill: Nearest Neighbors Algorithms (builds the initial graph), Spectral Clustering (supplies UMAP's initial layout), Isomap and Autoencoders (rival manifold methods), and Topological Data Analysis (the fuzzy-set theory underneath). For a friendlier language pass see the Hinglish note.