Principal Component Analysis
Imagine a spreadsheet with a thousand columns — every gene measured in a cell, every pixel of a
face, every sensor on an engine. A thousand numbers per sample is far too many to plot, to reason
about, or to feed cheaply into a model. Yet almost always those thousand numbers are not really
independent: they move together, they echo each other, they trace out a much thinner shape hiding
inside the vast space. Principal component analysis (PCA) is the tool that finds
that hidden shape — the handful of directions along which the data actually varies — and lets you
throw the rest away with almost no loss.
It is the workhorse of dimensionality reduction, and — wonderfully — it is nothing but
eigenvectors
of a symmetric matrix in disguise. Every idea you need is already built:
the covariance matrix
that records how features vary together, and the spectral theorem that guarantees its eigenvectors
are perpendicular. PCA simply reads those eigenvectors off, biggest first, and calls them the
principal components.
The recipe, in four steps
Lay your data in a matrix X with one row per sample and
one column per feature. PCA is then a short, fixed routine:
-
Centre it. Subtract each column's mean so the cloud sits at the origin. This step
is not optional — skip it and everything below breaks (see the "Watch out!" box).
-
Form the covariance matrix
C = \tfrac{1}{n-1} X^{\mathsf T} X (using the centred
X). It is d \times d for
d features, always
symmetric,
and positive semidefinite — no direction can have negative variance.
-
Diagonalize it. Find the eigenvalues
\lambda_1 \ge \lambda_2 \ge \cdots \ge \lambda_d \ge 0 and their
perpendicular eigenvectors \vec v_1, \vec v_2, \dots. These eigenvectors
are the principal components; each eigenvalue is the variance captured
along its direction.
-
Keep the top k. Project every sample onto
\vec v_1, \dots, \vec v_k. That gives the best possible
k-dimensional summary of the data — the one that loses the least, in the
exact sense of minimising reconstruction error.
The first eigenvector \vec v_1 points along the direction of
maximum variance — the long axis of the cloud. The second is the perpendicular
direction of next-most variance, and so on down the list. Because the matrix is symmetric, those
directions are automatically at right angles: a clean, ranked, orthogonal set of axes fitted to the
data itself.
Worked example 1: the long axis of a tilted cloud
Take a small centred dataset of five points already sitting at the origin:
(-2,-1),\ (-1,-1),\ (0,0),\ (1,1),\ (2,1). They lean along a diagonal —
as one coordinate grows the other tends to grow too. Build the
2\times2 covariance matrix with
C = \tfrac{1}{n-1}X^{\mathsf T}X, so we divide by
n-1 = 4:
\textstyle\sum x^2 = 4+1+0+1+4 = 10, \quad
\sum y^2 = 1+1+0+1+1 = 4, \quad
\sum xy = 2+1+0+1+2 = 6.
C = \frac{1}{4}\begin{bmatrix} 10 & 6 \\ 6 & 4 \end{bmatrix}
= \begin{bmatrix} 2.5 & 1.5 \\ 1.5 & 1 \end{bmatrix}.
Now find its top eigenvector — the first principal component. The
characteristic
equation is
(2.5-\lambda)(1-\lambda) - 1.5^2 = \lambda^2 - 3.5\lambda + 0.25 = 0,
giving \lambda = \tfrac{3.5 \pm \sqrt{12.25 - 1}}{2}, i.e.
\lambda_1 \approx 3.427 and
\lambda_2 \approx 0.073. For the larger eigenvalue, solving
(C - \lambda_1 I)\vec v = 0 gives the direction
\vec v_1 \approx (1,\ 0.618) — which points, exactly as promised, straight
along the long diagonal axis of the cloud. That single direction is what PCA would keep if you asked
it to squeeze this 2-D data down to one dimension.
Worked example 2: explained variance
How much did we actually keep? Each eigenvalue is a variance, so the fraction of the total spread
captured by component i — the explained variance ratio —
is simply
\text{explained}_i = \frac{\lambda_i}{\lambda_1 + \lambda_2 + \cdots + \lambda_d}.
Suppose a dataset's covariance matrix has just two eigenvalues, 9 and
1. Then the total variance is 9 + 1 = 10, and
the first principal component alone captures
\frac{9}{9+1} = 0.9 = 90\%.
Keeping only that one direction throws away a mere 10\% of the variance
while halving the number of dimensions. That is the whole promise of PCA in one line: when a few
eigenvalues dwarf the rest, a few directions carry nearly all the information. (For the tilted cloud
of example 1, the first component captures
3.427 / (3.427 + 0.073) \approx 98\% — the second direction is almost pure
thinness.)
See it: the data ellipse and its principal axes
Here is a centred cloud of correlated points. The two arrows are the principal
components — the eigenvectors of the cloud's covariance matrix — each drawn along its own
direction and scaled by \sqrt{\lambda}, the standard deviation of the data
along that axis. Together they trace the data ellipse. Turn up the correlation and
watch the long axis swing to follow the tilt; stretch each spread and watch the arrows grow. The
readout shows the two eigenvalues and how much variance the first component explains — crank the
correlation and the first component swallows nearly everything.
- The principal components are the eigenvectors of the covariance matrix
C = \tfrac{1}{n-1}X^{\mathsf T}X of the centred data.
- They are ordered by eigenvalue: \lambda_i is the
variance captured along \vec v_i, and
\vec v_1 is the direction of maximum variance.
- Being eigenvectors of a symmetric matrix, the components are mutually
perpendicular; projecting onto the top k minimises
reconstruction error.
PCA is just the SVD in disguise
You do not actually have to build C = \tfrac{1}{n-1}X^{\mathsf T}X and
diagonalize it. Run
the singular
value decomposition straight on the centred data matrix,
X = U\Sigma V^{\mathsf T}, and the principal components fall out for free:
- The right singular vectors (columns of V) are exactly
the principal components — the eigenvectors of X^{\mathsf T}X.
- The squared singular values are proportional to the eigenvalues:
\lambda_i = \sigma_i^2 / (n-1), so the largest singular value marks the
direction of greatest variance.
This is not just an algebraic curiosity — it is how PCA is computed in practice. Forming
X^{\mathsf T}X squares the condition number and loses numerical precision;
the SVD works directly on X and is far more stable. Keeping the top
k singular directions is precisely the best rank-k
approximation guaranteed by Eckart–Young. PCA, low-rank approximation, and the SVD are three views of
one idea: keep the strong directions, drop the weak ones.
The magic trick that lets a machine-learning engineer plot a thousand-dimensional dataset on a flat
screen is exactly this. Stack every sample as a row, centre the columns, and take the top
two eigenvectors of the covariance matrix. Project every sample onto just those two
directions and you get an (x, y) pair per sample — a 2-D scatter plot of
1000-D data, arranged so that the two axes on screen are the two directions along which the data
genuinely varies most.
Startlingly often this works: images of faces cluster by identity, handwritten digits separate into
blobs, gene-expression profiles split by tissue type — all visible in two dimensions that PCA
distilled from thousands. It works whenever the data's real structure lives in a low-dimensional
subspace hiding inside the high-dimensional space, which is astonishingly common. The other 998
directions were mostly noise and redundancy, and the top two eigenvalues quietly told you so.
Two traps sink more PCA attempts than any others.
-
You must centre the data first. If you skip subtracting the mean, the covariance
computation is dominated by where the cloud sits rather than how it is shaped,
and the first "principal component" just points from the origin toward the cloud's centre —
telling you nothing about its structure. Always subtract the column means before anything else.
-
PCA finds maximum variance, which is only "most informative" when variance = signal.
PCA is unsupervised: it never sees your labels, so it can happily discard a low-variance
direction that happens to be the one that matters. It is also scale-sensitive — a
feature measured in millimetres will dominate one measured in metres purely because its numbers are
bigger, not because it is more important. When features have different units, standardise
them first (divide each by its standard deviation) so the analysis compares like with
like.
Here is the one-line reason. The variance of the data projected onto a unit direction
\vec u is the quadratic form
\vec u^{\mathsf T} C\, \vec u. Maximising this over all unit vectors is a
classic constrained problem, and its answer — via the Rayleigh quotient — is the eigenvector of
C with the largest eigenvalue, whereupon the variance equals that
eigenvalue itself. The next-best direction, forced to be perpendicular to the first, is the
second eigenvector, and so on. So "the direction of greatest spread" and "the top
eigenvector of the covariance matrix" are not two facts that happen to coincide — they are the same
statement, read twice.