24 / 27 · Learning Without Labels
Dimensionality reduction
Describing high-dimensional data by a few coordinates: principal component analysis derived both as variance maximization and as minimal reconstruction error and computed through the SVD, explained variance and the choice of the number of components, whitening, probabilistic PCA, kernel PCA, and how to read t-SNE and UMAP maps, with autoencoders as the nonlinear generalization.
Mixtures and the EM algorithm · Kernels · The multivariate Gaussian · Linear algebra ↗
On this page 16
The mixture models of the previous note explained each point by a discrete latent variable, the cluster it came from. Much data is better explained by a few continuous ones. An image of a written digit has 64 pixel values, but the images vary along far fewer directions: which digit, its slant, its width, the thickness of the stroke. The points fill a thin region of , close to a low-dimensional subspace or surface. Finding that region gives coordinates that compress the data, remove noise, make distances more meaningful and let us look at the data in two dimensions. This note develops the linear answer, principal component analysis, from two directions that meet in one eigenvalue problem, gives it a probabilistic reading as a latent-variable model, and then turns to nonlinear methods that keep neighbourhoods rather than variance.
Notation. are the data points, centred so that , and is the matrix with rows . is the sample covariance matrix, with eigenvalues and orthonormal eigenvectors . is the target dimension, a matrix with orthonormal columns (), and the new coordinates of . is the singular value decomposition, with singular values .
Why reduce the dimension#
Four reasons, often at once:
- Looking. Plots have two axes. A map to two dimensions is the only way to see all 64 features of a dataset in one scatter plot.
- Compression. Storing coordinates instead of saves memory and makes everything downstream faster.
- Denoising. If the signal lives in a few directions and the noise is spread over all of them, discarding the weak directions removes mostly noise.
- Better distances. In high dimension, distances between points concentrate around a common value (the shell of the Gaussian), which hurts nearest neighbours and the clustering methods of this section. Distances in a well-chosen low-dimensional space are more informative.
PCA as variance maximization#
Project the data onto a single direction, a unit vector . The projections have mean zero, because the data are centred, and variance
A direction that keeps the points spread out keeps the differences between them, so ask for the direction of largest variance: maximize subject to . The Lagrangian has gradient , which vanishes when
So must be an eigenvector of , and then the variance is : the best direction is the eigenvector with the largest eigenvalue, , the first principal component. The second component is the direction of largest variance orthogonal to the first. Writing any unit in the eigenbasis, with , gives ; with forced by orthogonality, the maximum is , at . Continuing, the leading eigenvectors are the principal components, and the variance of the data along is exactly .
The multivariate Gaussian note met the same eigenvectors as the axes of the covariance ellipse. PCA is that geometry estimated from data: the principal components are the axes of the ellipse that best describes the cloud, ordered by length.
PCA as reconstruction-error minimization#
Now ask a different question. Represent each point by numbers and reconstruct it as . Which makes the reconstructions closest to the data? Because is an orthogonal projection, the residual is orthogonal to , and by Pythagoras
Averaging over the points,
The first term is the total variance, fixed by the data; the second is the variance kept by the projection. Minimizing the reconstruction error is therefore the same problem as maximizing the kept variance, and its solution is the same: , with minimal error
the sum of the discarded eigenvalues. (That the leading eigenvectors maximize over all orthonormal follows from the same eigenbasis argument applied to each column, or from Ky Fan’s theorem.) The two derivations are the two historical origins of PCA: Pearson (1901) looked for the line and plane of closest fit, Hotelling (1933) for components of maximal variance.
Two sides of one problem: variance and reconstruction error
Worked example. For the covariance of the probability course, the eigenvalues are and and the first eigenvector is . The first component keeps of the variance, and reconstructing from it alone leaves a mean squared error of . The point gets the coordinate and the reconstruction ; the residual has squared length , and indeed .
Through the SVD#
Every matrix has a singular value decomposition , with and having orthonormal columns and diagonal with ( the rank). Then
an eigendecomposition: the principal components are the right singular vectors , the eigenvalues are , and the coordinates of all points are . Two reasons to compute PCA this way. Forming squares the condition number, so small eigenvalues lose precision, while the SVD works on directly. And for large data a truncated or randomized SVD computes only the leading components, without the full matrix.
The SVD also says that PCA is optimal in a stronger sense. By the Eckart–Young theorem (Eckart and Young, 1936), the best approximation of by any matrix of rank in the Frobenius norm is the truncated SVD , with error . The PCA reconstruction with is exactly : no rank- linear summary of the data does better. Truncated SVD of a word–document count matrix is latent semantic analysis, the baseline of the GloVe note.
Explained variance and choosing k#
The share of variance kept by the first components is the explained variance ratio
and one minus it is the relative reconstruction error. On the 1797 digit images of scikit-learn, 64 pixels each, the first two components explain 14.9% and 13.6% of the variance; 5 components explain half, 21 explain 90% and 29 explain 95%. With 21 components the mean squared reconstruction error per image is 116.3, the sum of the 43 discarded eigenvalues, as the derivation above predicts.
There is no single rule for . Common choices:
- A share of variance, such as 90% or 95%. Simple, but the threshold is arbitrary.
- The elbow of the scree plot, the eigenvalues in decreasing order: keep the components before the curve flattens into a floor of small, similar eigenvalues, which is what isotropic noise produces.
- The downstream task: choose by the validation error of the model that uses the components.
- The likelihood of probabilistic PCA (below), on held-out data or with a penalty like BIC.
How many components to keep
The figure shows the first two rules disagreeing. The data have three directions of signal and noise in all ten; the scree plot shows the three, but they explain only 89.9% of the variance, so a 90% rule keeps a fourth component that is pure noise.
Standardizing and whitening#
PCA depends on the units of the features. A feature measured in grams has a thousand times the standard deviation of the same feature in kilograms and dominates the first component. When features have unrelated units, standardize each to unit variance first; PCA then works with the correlation matrix instead of the covariance. When they share a unit, as pixels do, the raw covariance is usually right.
Whitening goes one step further and rescales each component to unit variance:
The new coordinates are uncorrelated with unit variance, , the sample version of the whitening map of the multivariate Gaussian note. Whitening helps methods that assume comparable, uncorrelated inputs, such as independent component analysis or distance-based methods. Its risk is the division by : directions with tiny eigenvalues, often pure noise, are amplified to the same scale as the signal, so whitening is combined with truncation to components or with a small constant added to each .
Probabilistic PCA#
PCA as described is a projection, not a model: it assigns no probability to a point. Probabilistic PCA (Tipping and Bishop, 1999) is the continuous analogue of the Gaussian mixture. A latent vector is mapped linearly into the data space and blurred by isotropic noise, with ; by the affine-map rule, , a Gaussian whose covariance is “rank plus noise”. Its maximum likelihood solution is PCA in disguise: the columns of span the leading eigenvectors, for any rotation , and the noise variance is the average of the discarded eigenvalues, (Bishop, 2006, Section 12.2.1). In the scree figure that average is , the true noise variance; for the digits with it is . The posterior mean of the latent vector is . It is not the vector of principal coordinates , because holds scaled and rotated loadings, not the orthonormal eigenvectors: as it tends to , the principal coordinates divided by their standard deviations and rotated by . What becomes the PCA projection is the reconstruction in data space, . The model can also be fitted by EM, which avoids the covariance and handles missing values, and because it is a density it gives a likelihood for choosing and a score for unusual points, the starting point of the next note.
Kernel PCA#
PCA finds linear structure only. Two concentric circles have no direction of large variance that separates them: every projection to a line mixes the rings. Kernel PCA (Schölkopf, Smola and Müller, 1998) runs PCA in a feature space reached through a kernel , as in the kernels note. The principal directions in feature space are combinations of the mapped points, , and the eigenvalue problem becomes one for the kernel matrix , centred in feature space,
where is the matrix with all entries and is scaled so that the direction has unit length, . The coordinate of a point on a component is , computed with kernels only. With the feature space contains , and , so the squared radius , which separates the circles, is a linear function there. In the Code section, a logistic regression on two ordinary principal components of the circles reaches a training accuracy of 0.502, chance, and on three kernel components 1.0. Both are fitted and scored on the same 400 points, so they show what each representation can express linearly, not a test score. Only the third kernel component separates the rings by itself: kernel PCA ranks directions by variance in feature space, not by usefulness. The price is an eigenproblem, and the components have no simple interpretation in the input space.
t-SNE and UMAP: neighbourhoods instead of variance#
For visualization, preserving large distances is often the wrong goal. A 2-D linear projection of the digits keeps the directions of largest spread, and the ten digit classes overlap heavily. t-SNE (van der Maaten and Hinton, 2008) asks instead that points close in the data stay close in the plane.
- Turn distances in the data into neighbour probabilities. For each point , , normalized over , where each bandwidth is chosen so that the distribution has a set perplexity, , roughly the number of neighbours that count; typical values are 5 to 50. Symmetrize: .
- Define the same kind of probabilities for points in the plane, with a heavy-tailed Student- kernel: , normalized over all pairs.
- Move the by gradient descent to minimize .
The asymmetry of the KL divergence decides what is preserved. A pair with large , true neighbours, placed far apart (small ) costs a lot; a pair with small placed close costs little. t-SNE keeps neighbourhoods and is free with everything else. The heavy tail answers the crowding problem: a point in 64 dimensions can have many equidistant neighbours, a point in the plane cannot, and the kernel lets moderately distant points be pushed far away instead of piling up in the middle. In the Code section, 58.4% of the 10 nearest neighbours of each digit image in 64 dimensions are among its 10 nearest neighbours in a t-SNE map, against 11.8% in the 2-D PCA projection.
UMAP (McInnes, Healy and Melville, 2018) follows the same plan with different choices: a weighted -nearest-neighbour graph in the data, a similar graph in the plane, and a cross-entropy between their edge weights optimized by stochastic gradient descent. It is much faster, scales to millions of points and can embed new points, and it is often said to keep more global structure; the reading rules below apply to it as well.
How to read such maps (Wattenberg, Viégas and Johnson, 2016):
- Hyperparameters really matter. The perplexity (for UMAP, the number of neighbours) changes the picture; look at several values.
- Cluster sizes mean nothing. t-SNE expands dense clusters and contracts sparse ones, so the area of a cluster says nothing about its spread in the data.
- Distances between clusters may mean nothing. Two clusters far apart in the map may be close in the data, and the other way round.
- Random noise can look clustered, especially at low perplexity.
- Shapes and topology need more than one plot. A ring or a line in the map may be an artefact of one run.
What a map shows reliably is which points are neighbours. It is for looking, not for measuring: its axes have no meaning, there is no map of new points in plain t-SNE, and its coordinates are poor features for a model. The autoencoder note reads t-SNE plots of learned codes with exactly these cautions, and the word embeddings note compares PCA and t-SNE projections of word vectors.
Autoencoders: nonlinear PCA#
PCA is the best linear encoder–decoder pair: , , trained to minimize squared reconstruction error. Training a linear network with a -unit bottleneck on the same loss by gradient descent reaches the same subspace: its minimum spans the principal subspace, and it has no other local minima, only saddle points (Baldi and Hornik, 1989). Replacing the two linear maps by neural networks, an encoder and a decoder , gives an autoencoder, which can follow a curved surface instead of a flat subspace. The autoencoder note of the generative models course builds convolutional autoencoders for images on this idea, and the probabilistic version, with the encoder as an approximate posterior, is the variational autoencoder, probabilistic PCA made nonlinear.
What changed#
| PCA | Probabilistic PCA | Kernel PCA | t-SNE / UMAP | Autoencoder | |
|---|---|---|---|---|---|
| Map | linear, | linear, with a density | linear in feature space | none: positions optimized | neural encoder and decoder |
| Preserves | variance, large distances | the same, plus a likelihood | variance in feature space | neighbourhoods | what reconstruction needs |
| New points | project | project | kernels with the training set | not in plain t-SNE | encode |
| Solution | SVD, exact | closed form or EM | eigenproblem | non-convex, random | non-convex, gradient descent |
| Use | compression, denoising, features | the same, density, missing data | nonlinear features | visualization only | representations, generation |
Limitations#
- Linear and variance-driven. PCA keeps the directions of largest variance, which need not be the directions that matter for a task; a small-variance direction can carry all the class information.
- Sensitive to scale and outliers. Units decide the components, and a single far point can capture one; robust PCA variants exist for that.
- Components are hard to interpret. Each is a mixture of all features, with signs fixed only up to .
- Nonlinear maps are for looking. t-SNE and UMAP distort sizes and distances, depend on hyperparameters and random seeds, and plain t-SNE gives no function for new points. UMAP has one (
transformin the umap-learn library), but the new positions inherit the same distortions; a pipeline that feeds an embedding to a model must fit it on the training split only. - The intrinsic dimension is a modelling choice. Every rule for encodes an assumption about what is signal and what is noise.
What you should remember#
- PCA projects centred data onto the leading eigenvectors of the covariance ; the variance along is .
- Maximizing kept variance and minimizing squared reconstruction error are the same problem, since ; the minimal error is .
- Compute PCA by the SVD : components , eigenvalues , coordinates ; by Eckart–Young no rank- approximation is better.
- Choose by explained variance, the scree elbow, the downstream task or the likelihood; the rules can disagree.
- Standardize features with different units; whitening rescales components to unit variance and amplifies noise directions.
- Probabilistic PCA, , has PCA as its maximum likelihood solution and the mean of the discarded eigenvalues.
- Kernel PCA is PCA of the centred kernel matrix: nonlinear components at the cost of an eigenproblem.
- t-SNE and UMAP preserve neighbourhoods; cluster sizes and gaps between clusters in their maps carry little information.
- A linear autoencoder learns the PCA subspace; a nonlinear one generalizes it.
Code#
PCA by the SVD, explained variance and reconstruction error on the digits, probabilistic PCA, whitening, kernel PCA, and neighbourhoods in PCA and t-SNE maps:
import numpy as np
from sklearn.datasets import load_digits, make_circles
from sklearn.decomposition import PCA, KernelPCA
from sklearn.manifold import TSNE
from sklearn.neighbors import NearestNeighbors
from sklearn.linear_model import LogisticRegression
# the population: eigenvalues of the covariance and the share of the first component
S = np.array([[2.0, 1.2], [1.2, 1.0]])
lam = np.linalg.eigvalsh(S)[::-1]
print(lam.round(3), round(lam[0] / lam.sum(), 3)) # [2.8 0.2] 0.933
# PCA of a sample through the SVD of the centred data matrix: the same as the eigenvectors of the covariance
rng = np.random.default_rng(0)
X = rng.multivariate_normal([0, 0], S, size=2000)
Xc = X - X.mean(axis=0)
U, s, Vt = np.linalg.svd(Xc, full_matrices=False)
ev = np.linalg.eigvalsh(Xc.T @ Xc / len(X))[::-1]
print((s ** 2 / len(X)).round(3), ev.round(3), np.abs(Vt[0]).round(3)) # [2.812 0.198] [2.812 0.198] [0.826 0.563]
# digits: 1797 images of 8 x 8 pixels; share of the first two components, components needed for 50%, 90%, 95%
D = load_digits().data
p = PCA().fit(D)
cum = np.cumsum(p.explained_variance_ratio_)
print(p.explained_variance_ratio_[:2].round(3), [int(np.searchsorted(cum, q) + 1) for q in (0.5, 0.9, 0.95)])
# [0.149 0.136] [5, 21, 29]
# reconstruction error with k components = the sum of the discarded eigenvalues (per image, with the 1/n covariance)
for k in (2, 10, 21):
pk = PCA(k).fit(D)
err = ((D - pk.inverse_transform(pk.transform(D))) ** 2).sum(axis=1).mean()
lam_n = p.explained_variance_ * (len(D) - 1) / len(D)
print(k, round(err, 1), round(lam_n[k:].sum(), 1), round(cum[k - 1], 3))
# 2 858.9 858.9 0.285 | 10 314.5 314.5 0.738 | 21 116.3 116.3 0.903
# probabilistic PCA: the noise variance is the mean of the discarded eigenvalues
p10 = PCA(10).fit(D)
print(round(p10.noise_variance_, 3), round(p.explained_variance_[10:].mean(), 3)) # 5.828 5.828
# whitening: the rescaled coordinates have identity covariance
Z = PCA(10, whiten=True).fit_transform(D)
print(np.allclose(np.cov(Z.T), np.eye(10))) # True
# kernel PCA on two concentric circles: training accuracy of a linear classifier on the components
Xr, yr = make_circles(n_samples=400, factor=0.4, noise=0.05, random_state=0)
acc = lambda z: round(LogisticRegression().fit(z, yr).score(z, yr), 3)
z_lin = PCA(2).fit_transform(Xr)
z_ker = KernelPCA(3, kernel='poly', degree=2, gamma=1, coef0=0).fit_transform(Xr) # k(x, y) = (x . y)^2
print(acc(z_lin), acc(z_ker), [acc(z_ker[:, [j]]) for j in range(3)]) # 0.502 1.0 [0.53, 0.535, 1.0]
# neighbourhoods: share of each image's 10 nearest neighbours in 64-D that stay among its 10 nearest in 2-D
def kept(A, B, k=10):
ia = NearestNeighbors(n_neighbors=k + 1).fit(A).kneighbors(A, return_distance=False)[:, 1:]
ib = NearestNeighbors(n_neighbors=k + 1).fit(B).kneighbors(B, return_distance=False)[:, 1:]
return np.mean([len(set(a) & set(b)) / k for a, b in zip(ia, ib)])
for name, E in (('pca', PCA(2).fit_transform(D)), ('t-sne', TSNE(2, perplexity=30, random_state=0).fit_transform(D))):
print(name, round(kept(D, E), 3)) # pca 0.118 | t-sne 0.584
Next#
Probabilistic PCA and the Gaussian mixture are densities: they say not only where the data are, but how probable each point is. A point that the model finds very improbable, or that lies far from the subspace where the data live, is unusual. The next note turns that observation into methods for finding anomalies: density scores, distances to neighbours, random trees that isolate odd points quickly, and one-class support vector machines, and asks how to evaluate a detector when anomalies are rare.
References#
- K. Pearson. On lines and planes of closest fit to systems of points in space. The London, Edinburgh, and Dublin Philosophical Magazine and Journal of Science, 2(11), 559–572, 1901.
- H. Hotelling. Analysis of a complex of statistical variables into principal components. Journal of Educational Psychology, 24(6), 417–441, 1933.
- C. Eckart, G. Young. The approximation of one matrix by another of lower rank. Psychometrika, 1(3), 211–218, 1936.
- M. E. Tipping, C. M. Bishop. Probabilistic Principal Component Analysis. Journal of the Royal Statistical Society B, 61(3), 611–622, 1999.
- B. Schölkopf, A. Smola, K.-R. Müller. Nonlinear Component Analysis as a Kernel Eigenvalue Problem. Neural Computation, 10(5), 1299–1319, 1998.
- L. van der Maaten, G. Hinton. Visualizing Data using t-SNE. JMLR, 9, 2579–2605, 2008.
- L. McInnes, J. Healy, J. Melville. UMAP: Uniform Manifold Approximation and Projection for Dimension Reduction. arXiv:1802.03426, 2018.
- M. Wattenberg, F. Viégas, I. Johnson. How to Use t-SNE Effectively. Distill, 2016.
- P. Baldi, K. Hornik. Neural networks and principal component analysis: Learning from examples without local minima. Neural Networks, 2(1), 53–58, 1989.
- C. M. Bishop. Pattern Recognition and Machine Learning. Springer, 2006. Section 12.1 (PCA, both formulations), Section 12.2 (probabilistic PCA; 12.2.1, the maximum likelihood solution), Section 12.3 (kernel PCA).
- M. P. Deisenroth, A. A. Faisal, C. S. Ong. Mathematics for Machine Learning. Cambridge University Press, 2020. Chapter 10 (dimensionality reduction with PCA), Section 4.5 (the SVD), Section 4.6 (matrix approximation).
- E. Sokolov et al. Машинное обучение (HSE): lecture notes, lecture 12: unsupervised learning (2021); problems on PCA (2025).