Skip to content
Tech Interview Prep
Technical interview guideNew

UMAP and Nonlinear Embeddings

How UMAP builds a fuzzy k-nearest-neighbour graph and lays it out in low dimensions, how its parameters change the picture, and how to read the result without over-interpreting it.

Minutes to read
3
Practice MCQs
25
Interview Q&A
25
Edition
v2
Editorial status
Reviewed

Scope: umap-learn 0.5.x (Python package, current 0.5 line) and the algorithm as published by McInnes, Healy and Melville (arXiv 1802.03426, 2018). Parameter defaults (n_neighbors=15, min_dist=0.1, n_components=2, metric='euclidean') are those of umap-learn 0.5.x; verify against the release in use before quoting them..

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

wij=exp⁡(−max⁡(0, dij−ρi)σi)w_{ij}=\exp\left(-\frac{\max(0,\,d_{ij}-\rho_i)}{\sigma_i}\right) wijsym=wij+wji−wij wjiw^{\text{sym}}_{ij}=w_{ij}+w_{ji}-w_{ij}\,w_{ji} ∑j∈kNN(i)wij≈log⁡2k\sum_{j\in\mathrm{kNN}(i)} w_{ij}\approx\log_2 k

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

Input matrix PCA to about 30-100 dims kNN graph with n_neighbors Per-point rho and sigma, fuzzy weights Symmetrise with fuzzy union SGD: attract edges, sampled repulsion 2D or 3D coordinates
  • 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

SituationUse UMAP?Better or required step
Curved manifold, local neighbourhoods wanted in 2DYesPCA keeps global variance but folds local structure
Thousands of noisy raw featuresOnly after PCA to about 50Check explained variance first
Batch check, naming the axis and variance shareNoPCA, colour PC scores by batch, report the axis and its variance share
Distances or cluster sizes as factsNoQuantitative test in feature space
New cells arriving dailyYes, via transform on a frozen layoutRefit only when you accept the layout moving
Final cluster labelsCluster in PCA or graph spaceUse UMAP for display only

Traps interviewers probe

  1. Reading gaps and sizes as truth. Fix: gaps are not distances and density is not cluster size. Test separation in the feature space.
  2. Trusting one seed. Fix: run several random_state values and n_neighbors settings, and keep only what survives.
  3. Passing y and calling islands separable. Fix: supervised UMAP imposes the separation. Test with an unsupervised run and a classifier on raw features.
  4. Fitting UMAP on all rows, then cross-validating. Fix: fit on training data only, and use transform for held-out rows.
  5. 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:

  1. PCA to about 50 components, then UMAP with n_neighbors 15, using cosine for embeddings.
  2. UMAP builds a fuzzy kNN graph with a per-point scale and lays it out by attraction on edges and sampled repulsion.
  3. Local neighbourhoods are trustworthy. Gaps, sizes and counts are not.
  4. 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.