Exercises — UMAP for dimensionality reduction
Before we start, one shared reminder of the four objects every problem leans on. If you cannot say what each is in a sentence, stop and re-read the parent.
The picture below is the one mental image the whole page rests on.

Level 1 — Recognition
Problem 1.1 (L1)
For a point , its nearest neighbour sits at distance . What is the edge weight to that nearest neighbour , regardless of the value of ?
Recall Solution
What we compute: plug into the formula. Why it matters: subtracting guarantees the closest neighbour always gets weight exactly — no point is ever left isolated. The answer is for any .
Problem 1.2 (L1)
Name the two low-dimensional-embedding hyperparameters that control (a) the local-vs-global balance and (b) how tightly points may pack. State one typical value of each.
Recall Solution
- (a) ==
n_neighbors== (), typical value in . Larger → more global structure. - (b) ==
min_dist==, typical value in . Largermin_dist→ points forced apart.
Problem 1.3 (L1)
In the loss, UMAP uses cross-entropy, related to Cross-Entropy Loss. In one sentence, why cross-entropy rather than plain squared error?
Recall Solution
The fuzzy weights and live in and behave like membership probabilities; cross-entropy is the natural "distance between probability distributions," and — crucially — it punishes both a missing connection (attraction term) and a false connection (repulsion term), which squared error blends together.
Level 2 — Application
Problem 2.1 (L2)
Point has and solved bandwidth . A neighbour sits at . Compute to 4 decimals.
Recall Solution
Step — shift: . Step — scale: divide by : . Step — decay: . What it looks like: on figure s01 this is the height of the curve exactly one past the foot — the classic "" drop.
Problem 2.2 (L2)
Two directed weights come out and . Compute the symmetrized weight .
Recall Solution
Step: . Why this formula: it is the fuzzy set union (probabilistic OR): " connects to " OR " connects to ." The subtracted product stops double-counting. Answer , and it can never exceed .
Problem 2.3 (L2)
Low-D kernel is with , . Two embedded points are at Euclidean distance . Compute to 4 decimals.
Recall Solution
Step: at distance , . What it looks like: see figure s02 — this heavy-tailed curve stays high near and fades slowly, which is exactly what lets clusters separate without every point crushing to the origin.
Level 3 — Analysis
Problem 3.1 (L3)
Point has neighbours with distances , so . The bandwidth condition is . Verify whether satisfies it, and state which direction binary search must move next.
Recall Solution
Target: Weights at :
- : → .
- : → .
- : → . Sum: . Compare: , so the sum is too big. Direction: the weights decay too slowly ⇒ ==decrease == to sharpen the decay and shrink the sum. So is not the answer; binary search steps the bandwidth down.
Problem 3.2 (L3)
Using the same neighbour distances , , does come much closer to the target ? Compute the sum and comment.
Recall Solution
Weights at :
- : .
- : .
- : . Sum: , versus target . Comment: the sum is within of target — binary search would nudge down a hair more, but this is already a good approximation. This shows how each point gets its own tuned to its local density: dense regions get small , sparse regions get large . That per-point calibration is UMAP's answer to varying data density, and is what distinguishes it from a fixed-radius method like plain Isomap.
Problem 3.3 (L3)
A student sets n_neighbors = 2. Explain, using the condition, why the resulting graph is dangerously sparse and what visual artefact appears.
Recall Solution
The condition at : target . But the nearest neighbour alone already contributes . So the sum must equal with the first term already — forcing the second neighbour's weight toward and . Consequence: each point effectively connects to only its single nearest neighbour. The graph becomes a loose collection of near-isolated edges, easily fragmenting one true manifold into many disconnected shards. Visual artefact: the embedding shatters a smooth structure into scattered small clumps that carry no global relationship — you lose exactly the global structure UMAP was chosen to keep.
Level 4 — Synthesis
Problem 4.1 (L4)
You must reduce single-cell RNA-seq profiles ( genes each) to 2-D. You want (i) tight, clearly separated cell-type clusters and (ii) the inverse ability to map a brand-new cell into the same 2-D plot later. Choose between PCA, t-SNE, and UMAP, set n_neighbors and min_dist, and justify each choice from the mechanisms above.
Recall Solution
Method: UMAP.
- PCA is linear — it projects, so a curved manifold (cell-state trajectory) gets flattened and clusters overlap. Rejected for (i).
- t-SNE cannot embed new points without a full re-run — it fails requirement (ii) outright.
- UMAP builds a reusable model, so a new cell can be pushed through — satisfies (ii).
n_neighbors: small–moderate, e.g. . From Problem 3.2's logic, small tightens and emphasises local structure → sharp, separated clusters as (i) demands.min_dist: small, e.g. . In the kernel , a smallmin_distlets as , so points are allowed to pack tightly — preserving within-cluster density. Cross-check: if instead we wanted an airy visualization for a slide, we'd raisemin_distto – to inflate the layout, trading density detail for readability.
Problem 4.2 (L4)
Explain how UMAP's optimization uses negative sampling and why, without it, the embedding would collapse. Connect this to the second (repulsion) term of the cross-entropy loss.
Recall Solution
The two forces in the loss:
- First term = attraction between genuinely-connected pairs.
- Second term = repulsion that pushes apart pairs the high-D graph says are not connected. The problem: there are non-edges — computing every repulsion is infeasible. Negative sampling: at each gradient step, sample a handful of random "non-neighbour" pairs and apply only their repulsion. This is an unbiased cheap estimate of the full second term. Why collapse otherwise: with only attraction and no repulsion, every point is pulled toward every neighbour with nothing pushing back — the whole embedding contracts to a single point. Negative sampling supplies the outward pressure that gives clusters room. (Same spirit as the repulsive term you meet in Spectral Clustering eigen-embeddings that spread points across an eigen-axis.)
Level 5 — Mastery
Problem 5.1 (L5)
Design a pipeline for embedding million points where even UMAP's neighbour search is a bottleneck. You may combine any of: Random Projection, Nearest Neighbors Algorithms, PCA, UMAP. Give the ordered stages and justify each with the mechanism it accelerates or preserves.
Recall Solution
Stage 1 — Random Projection or PCA (dimension pre-reduction). Cut the ambient dimension (e.g. ). By the Johnson–Lindenstrauss idea, random projection approximately preserves pairwise distances, so the neighbour graph UMAP builds next is nearly unchanged — but each distance now costs far less to compute.
Stage 2 — approximate Nearest Neighbors Algorithms (e.g. NN-Descent / ANN). UMAP already uses approximate NN internally; ensure the index is the sub-quadratic kind. This produces the -NN graph that feeds .
Stage 3 — UMAP with spectral initialization + negative sampling. The spectral init (a Spectral Clustering-style eigen-layout) gives a good starting embedding, cutting the epochs SGD needs. Negative sampling keeps each epoch linear in edges.
Stage 4 — reuse the fitted model to transform future streaming points without refitting.
Justification thread: every stage attacks one cost — ambient dimension (S1), neighbour search (S2), optimization iterations + repulsion cost (S3) — while each preserves the quantity the next stage relies on (distances, then graph, then layout).
Problem 5.2 (L5)
Steel-man the claim "UMAP preserves global structure" and then bound it: give one concrete scenario where UMAP's between-cluster distances are not trustworthy, and tie it to a specific mechanism on this page.
Recall Solution
The steel-man: through the calibration () plus a spectral (global) initialization plus negative-sampling repulsion, UMAP retains more large-scale geometry than t-SNE, which explicitly discards it. Empirically, UMAP often lays out related clusters near each other. The bound / failure case: the low-D kernel is heavy-tailed and saturating: once two clusters are "far," pushing them from distance to barely changes (both ), so the loss gradient there is nearly flat. Therefore the magnitude of a large inter-cluster gap in the plot is essentially unconstrained — two clusters shown "very far" apart may be only slightly less related than two shown "moderately far." Rule: trust ordering and neighbourhood of clusters, distrust exact distances between distant clusters. (For faithful global distances you'd instead reach for Isomap or Topological Data Analysis summaries.)
Recall Self-test checklist (reveal to grade yourself)
Can you, without notes: (1) plug numbers into including the clamp? (2) symmetrize two weights? (3) say which way moves when the sum overshoots? (4) name why UMAP (not t-SNE/PCA) supports inverse transform? (5) state exactly which distances in a UMAP plot are not trustworthy and why? If yes to all five — L5 reached. Which term supplies the outward push that stops collapse? ::: The repulsion (second) cross-entropy term, estimated by negative sampling. If is above , does go up or down? ::: Down.