Overview
Curated: · Written: · Reviewed:
UMAP and Nonlinear Embeddings
In one line
UMAP builds a fuzzy k-nearest-neighbour graph on the input, then lays it out in 2D or 3D so those local neighbourhoods are roughly preserved. Reach for it after PCA, when you need a local picture of data that lies on a curved manifold, such as a Swiss roll, where a linear projection folds distant parts together.
Key definitions
n_neighbors: size of the local neighbourhood. Default 15 in umap-learn 0.5.x.- Local connectivity (rho): distance from a point to its nearest neighbour. That edge gets weight 1, so every point keeps one strong edge.
- Bandwidth (sigma): a per-point scale that makes the neighbour weights sum to about log2(k).
- Fuzzy union: symmetrises directed edges as a + b - ab.
min_dist: controls how tightly embedded points may pack. It does not change the graph.transform: places new points relative to a fitted layout. It cannot create new structure.
Formulas and complexity
For the nearest neighbour, d equals rho, so the weight is 1. Neighbour search is the main cost at large n and high d. Layout cost grows roughly with the number of edges. Quote no runtime figures you have not measured.
How it works
- Phase one decides who the neighbours are. Phase two sees only graph weights, never raw distances.
- The nearest neighbour gets weight 1, which keeps sparse regions connected.
- The layout minimises cross-entropy between the input graph and the embedding graph, using SGD.
- Repulsion is sampled from non-neighbours, which is cheaper but gives less exact global spacing.
When to use and when not to
| Situation | Use UMAP? | Better or required step |
|---|---|---|
| Curved manifold, local neighbourhoods wanted in 2D | Yes | PCA keeps global variance but folds local structure |
| Thousands of noisy raw features | Only after PCA to about 50 | Check explained variance first |
| Batch check, naming the axis and variance share | No | PCA, colour PC scores by batch, report the axis and its variance share |
| Distances or cluster sizes as facts | No | Quantitative test in feature space |
| New cells arriving daily | Yes, via transform on a frozen layout | Refit only when you accept the layout moving |
| Final cluster labels | Cluster in PCA or graph space | Use UMAP for display only |
Traps interviewers probe
- Reading gaps and sizes as truth. Fix: gaps are not distances and density is not cluster size. Test separation in the feature space.
- Trusting one seed. Fix: run several random_state values and n_neighbors settings, and keep only what survives.
- Passing y and calling islands separable. Fix: supervised UMAP imposes the separation. Test with an unsupervised run and a classifier on raw features.
- Fitting UMAP on all rows, then cross-validating. Fix: fit on training data only, and use transform for held-out rows.
- Calling arms or islands biology. Fix: check markers, QC metrics and batch before naming a population or a path.
Interview tips
Open with: 'UMAP preserves the local neighbour graph, not distances, so I read the plot as local neighbourhoods under these parameters.'
Follow-ups to expect:
- What n_neighbors does at small and large values, and which to start from for 50,000 unlabelled points.
- Why min_dist changes only packing.
- Why random_state fixes repeatability and disables parallel layout.
- How transform places new points, and when those placements are unreliable.
- Why cosine is preferred for embeddings with meaningful direction but varying norms.
30-second outline:
- PCA to about 50 components, then UMAP with n_neighbors 15, using cosine for embeddings.
- UMAP builds a fuzzy kNN graph with a per-point scale and lays it out by attraction on edges and sampled repulsion.
- Local neighbourhoods are trustworthy. Gaps, sizes and counts are not.
- Any group gets confirmed against markers, feature-space tests, and stability across seeds and n_neighbors before I name it.
Feedback on this overview
Log in free to save your progress and keep bookmarks and notes for a quick revision before your interview.