Machine Learning

Dimensionality Reduction for High-Dimensional Data

How and when to use PCA, t-SNE and UMAP to explore complex datasets, and what each plot can and cannot tell you.

By Nijiati Abulizi2 min read

Reducing thousands of variables to two is how we get to look at high-dimensional data at all. It is also where a lot of over-interpretation begins. The three methods most people use answer different questions, and the plots should be read differently.

PCA: the honest baseline

Principal component analysis finds the directions of greatest variance. It is linear, fast and deterministic, and its axes mean something: each component is a weighted sum of the original variables, and you know how much variance it explains. Use it first, every time. It is the best tool for spotting outliers and batch effects, and distances in a PCA plot are real distances in a rotated space.

t-SNE: neighbourhoods, not geography

t-SNE places points so that close neighbours stay close. It is excellent at revealing local clusters. What it does not preserve is anything global: the size of a cluster and the distance between two clusters are largely artefacts of the algorithm and of the perplexity setting. Two well-separated blobs may or may not be far apart in the data.

UMAP: faster, a little more global

UMAP is built on a similar neighbour-graph idea, runs faster on large data, and tends to keep more of the broad arrangement. That makes it tempting to read as a map. It is still not one: the result depends on the number of neighbours and the minimum distance, and inter-cluster distances remain unreliable.

A workflow that holds up

from sklearn.preprocessing import StandardScaler
from sklearn.decomposition import PCA
import umap

X_scaled = StandardScaler().fit_transform(X)
X_pca = PCA(n_components=50, random_state=0).fit_transform(X_scaled)

embedding = umap.UMAP(
    n_neighbors=15, min_dist=0.1, random_state=0
).fit_transform(X_pca)
  • Scale the variables, then reduce with PCA before t-SNE or UMAP. It removes noise and speeds things up.
  • Fix the random seed, and rerun with a few different settings. Structure that survives is worth believing.
  • Colour the plot by batch as well as by biology.

What not to do

  • Do not cluster on the 2D embedding. Cluster in the original or PCA space, then use the embedding to display the result.
  • Do not read meaning into gaps or cluster sizes in t-SNE or UMAP.
  • Do not fit the embedding on all the data and then evaluate a classifier on it. The test samples have already shaped the features. Fit on training data and transform the rest.

Used this way, dimensionality reduction is what it should be: a way of asking better questions of the data, not a result in itself.

All insights