Contents
05Dimensionality reduction: principal component analysis
The second classic unsupervised task is to describe data with fewer numbers: hundreds of features or millions of pixels usually vary in far fewer independent ways. Principal component analysis (PCA) finds the directions in which the data vary most.
5.1Intuition: photographing a pencil
Suppose you must photograph a pencil and show as much of it as possible in a single flat picture. Photographed end-on it is a dot; photographed from the side it shows its full length. PCA chooses the viewing direction in the same way: among all directions onto which we could project (flatten) the data, it picks the one along which the projected points are most spread out, so that as little as possible is lost.
5.2Two definitions that give the same answer
Centre the data, so that the mean \bar x=\frac1n\sum_i x_i is zero (otherwise replace x_i by x_i-\bar x below), and consider projecting onto a line through the origin with unit direction u, \|u\|=1. Point x_i projects to (u^\top x_i)\,u, at coordinate u^\top x_i along the line. There are two natural ways to choose u, and historically both were proposed:
- Closest fit (Pearson, 1901): choose the line that passes closest to the points, minimizing the sum of squared perpendicular distances \sum_i\|x_i-(u^\top x_i)u\|^2.
- Maximum variance (Hotelling, 1933): choose the direction along which the projected coordinates vary most, maximizing \frac1n\sum_i (u^\top x_i)^2.
They are the same problem. For each point, the projection and the residual are perpendicular, so by Pythagoras’ theorem
Summing over the points, the left side, the total spread of the data, does not depend on u. So variance kept plus squared error lost is constant, and maximizing the first is the same as minimizing the second. The right panel of Figure 4 shows this trade for every direction.
5.3The derivation: an eigenvalue problem
Write the variance along u with the covariance matrix \Sigma=\frac1n\sum_i x_ix_i^\top (or with \frac{1}{n-1}, which changes nothing below):
We maximize u^\top\Sigma u subject to u^\top u=1; without the constraint we could make the variance as large as we like by lengthening u. With a Lagrange multiplier \lambda,
So the best direction must be an eigenvector of the covariance matrix. Multiplying \Sigma u=\lambda u on the left by u^\top gives u^\top\Sigma u=\lambda: the variance along an eigenvector is its eigenvalue. The maximum is therefore attained at the eigenvector with the largest eigenvalue, the first principal component. Because \Sigma is symmetric and positive semidefinite, its eigenvalues \lambda_1\ge\lambda_2\ge\dots\ge\lambda_d\ge0 are real and its eigenvectors can be chosen orthonormal (the spectral theorem). Repeating the argument in the directions orthogonal to the first component gives the second, and so on: the top k eigenvectors span the best k-dimensional subspace. The variance kept is \lambda_1+\dots+\lambda_k, the squared reconstruction error is \lambda_{k+1}+\dots+\lambda_d, and the fraction of variance explained is
Eckart and Young (1936) proved the matrix version: the best rank-k approximation of the centred data matrix in squared error comes from its top k singular vectors, which is how PCA is computed in practice (via the singular value decomposition X=USV^\top; the eigenvalues of \Sigma are s_j^2/n).
Worked example 3. The data of the video’s PCA animation (140 points) have covariance matrix
Its eigenvalues solve \det(\Sigma-\lambda I)=\lambda^2-\tr(\Sigma)\,\lambda+\det\Sigma=0, with \tr\Sigma=2.691 and \det\Sigma=2.018\times0.673-0.963^2=0.431:
An eigenvector for \lambda_1 solves (2.018-2.520)u_1+0.963\,u_2=0, so u\propto(0.963,\,0.502); normalized, u=(0.887,\,0.462), a direction 27.5^\circ above the horizontal (the simulation stretched the data along 28^\circ). The first component keeps 2.520/2.691=93.6\% of the variance, so describing each point by one number instead of two loses only 6.4\% of the spread.
5.4Using PCA well
Centre the data, or the first component points at the mean. Standardize features with different units: PCA chases variance, so a feature in grams instead of kilograms would dominate for no reason. And high variance is not usefulness: PCA never sees labels, and the direction that separates two classes can have low variance. A famous application is eigenfaces (Turk and Pentland, 1991), which described face images by their coordinates along the top principal components of a set of training faces. A new face is recognized by projecting it onto these eigenfaces and comparing its few coordinates with those of known faces, instead of comparing thousands of pixels. PCA is linear; for visualizing curved structure, t-SNE (van der Maaten and Hinton, 2008) and UMAP (McInnes et al., 2018) are popular, though distances and cluster sizes in their pictures are not reliable. An autoencoder, a network that reconstructs its input through a narrow middle layer, generalizes PCA: with linear layers and squared error it learns the same subspace (Baldi and Hornik, 1989).
Pause and think. Your data have 50 features and the first two principal components explain 98% of the variance. Does that mean a classifier can use those two components and lose almost nothing?
Show the answer
Not necessarily. They keep almost all of the variance, but the label may depend on a low-variance direction. Check with validation accuracy, not with explained variance.