Dimensionality reduction
Turning a thousand columns into two without losing what matters: PCA, UMAP, and why a beautiful map supports few conclusions.
The idea
An object casts a shadow. The shadow has two dimensions instead of three and is still often recognisable. Dimensionality reduction looks for the angle from which the shadow reveals most.
What it is good for
- Condensing a thousand features into fifty so a model trains faster.
- Drawing a dataset on two axes to get any impression at all.
- Removing noise by discarding the weakest directions.
Which method when
| Purpose | Method | Why |
|---|---|---|
| Preprocessing for a model | PCA | Linear, fast, applies to new data |
| Removing noise | PCA, truncated SVD | Weak directions are mostly noise |
| Display, local structure | UMAP | Preserves neighbourhoods well |
| Display, small datasets | t-SNE | Separates groups clearly, slow |
| Very sparse matrices | Truncated SVD | Does not require centring the matrix |
import numpy as np
from sklearn.decomposition import PCA
from sklearn.preprocessing import StandardScaler
rng = np.random.default_rng(0)
X = StandardScaler().fit_transform(rng.normal(size=(2000, 60)))
p = PCA().fit(X)
cum = np.cumsum(p.explained_variance_ratio_)
print("components for 95 % variance:", int(np.searchsorted(cum, 0.95) + 1))
# Always run PCA on standardised data, otherwise the column with the
# largest unit determines the first principal component.PCA formally
In practice you compute the singular value decomposition X = U S Vᵀ rather than
the eigendecomposition of XᵀX: numerically more stable, and V contains the
principal components directly.
The curse of dimensionality, quantified
For uniformly distributed points in the d-dimensional unit cube,
At d = 2 that ratio is typically several hundred percent; at d = 100 it is in
the single-digit percent range. Practical consequence: nearest-neighbour search
on raw high-dimensional features says little. Embeddings escape this because
their points are not uniformly distributed but lie on a far lower-dimensional
manifold.
Reading UMAP maps properly
- Neighbourhoods are usually reliable, global distances are not.
- The size of a group on the map says nothing about its spread in the original space.
- Two runs with different seeds give different pictures. Always look at several.
- Never cluster on the map and report the result as a finding, cluster in the original space and use the map only to show it.
Related courses and sources
MIT 18.065 Matrix Methods
Singular value decomposition, principal components and optimisation applied to data. The bridge between linear algebra and what models actually compute.
For the step from pure mathematics to what models actually compute.