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.

Read
14 min
Practice MCQs
25
Interview Q&A
25
Edition
v1
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

UMAP is the default tool for taking a few hundred to a few million high-dimensional points and drawing them in two dimensions. The reason interviewers care is not the plot. It is whether you know what the algorithm preserves, what it throws away, and why the picture lies in specific ways. Most wrong answers treat the 2D layout as a faithful map of distances. It is not. This guide builds the model first, then the parameters, then the reading rules.

The one model to carry: UMAP first decides who each point's neighbours are, then arranges the points in low dimensions so that those neighbourhoods are roughly preserved. Everything else (the parameters, the artefacts, the warnings about cluster sizes) follows from which part of that sentence is doing the work.

Why nonlinear embeddings exist and where PCA stops helping

PCA finds the orthogonal directions of greatest variance and projects onto the top few. It is linear, so it preserves global variance structure well and local neighbourhoods poorly. If your data lies on a curved surface (a Swiss roll is the standard toy case), a linear projection folds distant parts of the surface on top of each other and the neighbourhoods are destroyed.

The working assumption behind manifold methods is that high-dimensional data concentrates near a lower-dimensional manifold, and that local neighbourhoods on that manifold are what matter. UMAP's own documentation states three assumptions: the data is uniformly distributed on a Riemannian manifold, the Riemannian metric is locally constant (or approximately so), and the manifold is locally connected. You should be able to say what breaks when these fail. Sparse, clumpy data with no real local metric gives a layout that looks confident and means little.

The practical rule: use PCA to get to roughly 30 to 100 dimensions, then UMAP for the picture. PCA removes noise and makes the nearest-neighbour search tractable. Using UMAP straight on very wide, noisy data mostly finds noise neighbours.

The two-phase idea: a fuzzy neighbour graph, then a layout that preserves it

Phase one builds a weighted k-nearest-neighbour graph. For each point, UMAP finds its k nearest neighbours (default n_neighbors=15). It then sets a local distance scale per point, so that the nearest neighbour always gets membership strength 1. This is the local connectivity idea: every point is guaranteed at least one strong edge, which keeps the graph connected in sparse regions. Edge weights decay with distance, so each edge reads as a fuzzy membership, not a hard link. Symmetrised edges combine as a fuzzy union, a + b - a*b.

Phase two lays the graph out. UMAP defines a similar fuzzy weight in the embedding, and minimises cross-entropy between the two graphs using stochastic gradient descent. Attractive forces pull connected points together; repulsive forces, sampled from non-neighbours, push unrelated points apart. The output is a 2D or 3D coordinate per point.

Trace the mechanism with real numbers. Suppose a point's 15 neighbours sit at distances 0.8 to 2.1 in the input space. The nearest gets weight 1.0, and the rest get weights set by a smooth decay calibrated so the sum over neighbours matches log2(k). Those weights are what the layout tries to reproduce. Nothing in phase two sees the raw distances directly. That is why UMAP is a graph-preservation method, not a distance-preservation method.

What interviewers probe: why the local scale per point exists (adaptive density), and why the graph is fuzzy rather than a hard kNN graph. A weak answer says "it uses nearest neighbours to cluster".

The parameters and what each one changes in the picture

  • n_neighbors (default 15) sets the size of the local neighbourhood. Small values (2 to 5) emphasise fine local detail and fragment the picture into many small islands. Large values (50 to 200) favour global layout and smooth out local structure. This is the single parameter that most changes the story.
  • min_dist (default 0.1) controls how tightly points pack in the embedding. It does not change the graph; it changes how close embedded points are allowed to sit. Small values give dense clumps; larger values spread points and make structure look more like a continuum.
  • n_components (default 2) sets the output dimensionality. Three components are useful for density checks; higher values are rarely worth it for plotting.
  • metric (default euclidean) defines the input distance. Changing it changes what "neighbour" means more than any other setting. Cosine is the usual choice for text and embeddings.
  • random_state fixes the seed, and setting it disables parallelism. More on that below.

Worked comparison, hypothetical figures on a 50,000-cell dataset: at n_neighbors=5 you may see 40 small islands; at n_neighbors=50 the same data often collapses to 8 or 9 large groups. Neither picture is wrong. They answer different questions, and a candidate who can state which parameter produced which picture is ahead of most.

Local versus global structure and why a UMAP plot cannot be read literally

UMAP preserves local neighbourhoods well and global structure only loosely. Four readings are wrong:

  • Cluster size is not density. A tight cluster may be a large, dense group or a small group that min_dist packed tightly. Compare sizes only after checking density some other way.
  • Gaps are not distances. The space between two islands does not measure how different they are. Inter-cluster distances in the plot are not meaningful in absolute terms.
  • Cluster count is not ground truth. UMAP will draw structure in noise, and the number of visible groups moves with n_neighbors. Run two or three settings before claiming a count.
  • Relative position of distant groups can flip. Two groups that look adjacent may be far apart in the input, and vice versa.

The defensible claim is: points drawn close together were close in the input graph under these parameters. Anything beyond that needs a separate check.

Reproducibility, scaling and PCA first on wide data

UMAP uses stochastic optimisation and approximate nearest-neighbour search. Two runs with different seeds give different layouts, and their local neighbourhoods agree much more than their global arrangement. Setting random_state makes runs repeatable, but it turns off multithreaded layout, which costs time on large data. State that trade-off if asked.

On scale: the neighbour search is the main cost for large n and high d, and the optimisation is roughly linear in the number of edges. Reducing dimensionality with PCA first helps both speed and the quality of the neighbour graph. For wide data (thousands of features), do PCA to 50 or so, then UMAP. Check the explained variance first. If 50 components capture little of the variance, the problem is the input, not the reducer.

Transforming new points, supervised UMAP and comparison with t-SNE

UMAP supports transform on new points: it finds their neighbours in the training set and places them using the fitted embedding. This is an approximation. New points are placed relative to the existing layout, so they cannot create new structure, and a new point far from all training data will be placed somewhere arbitrary-looking. Do not present transformed points as independent evidence of a cluster.

Supervised UMAP passes labels in fit, which pushes points of the same label together in the graph. It separates labelled classes sharply, and that is the danger: a supervised plot shows the separation you asked for, so it cannot be used to show that the classes are naturally separable.

Compared with t-SNE: both are neighbour-graph methods for visualisation. t-SNE uses a perplexity-based probability model and tends to be slower on large data and to keep less global structure. UMAP is faster, scales better, and has a transform method. Neither is a clustering algorithm, and neither should be scored on the cluster labels it produces without a separate step.

Visualisation versus features for clustering, and common misreadings in single-cell plots

Using UMAP coordinates as features for clustering works, but it changes the result: you cluster on a lossy projection built with parameters you chose. The cleaner pattern is to cluster on the PCA or graph space and use UMAP only to display. Where you must cluster in the embedding, state the parameters and check stability across them.

Single-cell misreadings are the most common in practice. Typical ones: treating a small isolated island as a new cell type without checking marker genes; reading a trajectory-like curve as a developmental path, when the layout shape is an artefact of the neighbour graph; comparing cluster sizes across samples on a single plot, when sampling depth changes the picture; and treating a batch effect as biology because the batches separate in the layout. The fix is to verify any grouping against an independent signal such as markers, counts or a second method before naming it.

What a weak answer sounds like: "UMAP clusters the data and the clusters are the cell types." What a strong one sounds like: "UMAP shows local neighbourhood structure under these parameters; I'd confirm the groups against markers and check stability across n_neighbors before naming them."

Feedback on this overview