Q&A-1Your team needs a 2D picture of 1,500 points that lie on a curled sheet, like a Swiss roll. Why might PCA mislead you here, when would you reach for UMAP instead, and what must you not read off the UMAP plot?
Answer
The 90-second answer. PCA is the right first tool when the structure you care about is linear and the variance is where the signal is. It finds the orthogonal directions that maximise retained variance, so a 2D PCA plot is the best flat picture of the data's dominant linear structure. A Swiss roll breaks that assumption. The sheet is two-dimensional but curled inside three dimensions, so two points on opposite layers of the roll can sit close in Euclidean distance while being far apart along the sheet. A linear projection onto the best 2D plane drops one direction and stacks those layers on top of each other. The regions are distinct on the manifold and collapsed in the plot.
UMAP starts from the opposite assumption: the data lies on a manifold, and distances only mean something locally. It takes each point's k nearest neighbours (default n_neighbors=15), converts their distances into fuzzy membership strengths using a per-point bandwidth, and builds a weighted graph of the local neighbourhoods. It then optimises a 2D layout whose graph has similar edge strengths, pulling neighbours together and pushing non-neighbours apart with a cross-entropy objective. Because neighbourhoods are local, the two layers of the roll stay apart whenever no neighbour edge links them.
# umap-learn 0.5.1, scikit-learn 1.x, Python 3.11
from sklearn.datasets import make_swiss_roll
from sklearn.decomposition import PCA
import umap
X, t = make_swiss_roll(n_samples=1500, noise=0.0, random_state=0)
Z_pca = PCA(n_components=2).fit_transform(X)
Z_umap = umap.UMAP(n_neighbors=15, min_dist=0.1, random_state=0).fit_transform(X)
Plot t against each embedding. The PCA output folds the roll into an overlapping band. The UMAP output tends to unroll it, though this depends on the neighbourhood size.
What each method preserves. PCA preserves variance under a linear map, so global distances in the projection are faithful to the extent the top components carry the data. UMAP preserves local neighbourhood structure. It does not preserve global distances or densities, so inter-cluster gaps and cluster sizes in the plot are not quantities you can read. Two clusters that look far apart may be closer in the original space than the picture suggests, and a dense-looking island may be a sampling artefact.
Failure modes. The first is over-reading the layout. Treating UMAP coordinates as metric is the most common error. The second is parameter sensitivity: n_neighbors controls the balance between local and global structure, and min_dist controls how tightly points pack. The third is nondeterminism. UMAP is stochastic, so a fixed random_state is needed for reproducibility, and in umap-learn fixing it also disables parallel execution. Detect the first by a sanity check: compare nearest-neighbour rank in the input and the embedding, or run at two n_neighbors values and see what changes.
When not to use it. If the question is about variance, a downstream linear model, or a compressed input for another learner, PCA is the right answer. UMAP is for visual exploration and for clustering that depends on local density.
Curated: · Written: · Reviewed: