PCA and Eigenfaces: building a face from six numbers
A color image is a list of numbers. Yet you do not need numbers to represent a sharp human face — or a dog. Principal Component Analysis (PCA) finds a small set of template images, ordered by how much they vary across a dataset, so that moving a handful of sliders morphs smoothly across the space of faces.
Try it before reading a single equation. Switch between real human faces (CelebA-HQ) and aligned dog portraits (AFHQv2). Pick a preset #1–#4 (or mean to start from the average), then drag the eight sliders — or drag horizontally across any of the eight eigen-images on the right — to reshape the portrait in real time.
Interactive color eigenface and eigendog mixer: a reconstructed 96×96 RGB portrait beside the top eight principal component templates; eight sliders and preset buttons morph the image linearly from the dataset mean.
Every pixel on the left is computed live by one linear formula:
Here is the mean image (press mean to see it with every weight set to zero), each is a unit-length principal component (an eigenface or eigendog), is the standard deviation along that axis, and is the slider value in units of . Clicking #1–#4 loads a real portrait's top coordinates so fine detail stays sharp while the eight sliders move PC1–PC8 live; clicking mean zeroes all weights so you can build an image purely from the first eight axes.
With only eight numbers out of , those eight axes already account for of the total RGB variance across all human faces (and across the dogs). Notice what the first few components actually discover on their own, without anyone labelling a single feature: overall brightness and background contrast, skin and hair tone, directional lighting, head turn, expression, and facial structure.
An image is a point in high-dimensional space
Flatten the RGB channels of an image end to end and you get a single vector with . A dataset of images is a cloud of points in , stored as rows of a matrix of shape .
Almost the entire volume of is television static — independent random pixels that look like nothing. Aligned portraits occupy a tiny, thin neighborhood inside that space because neighboring pixels are strongly correlated: if the left cheek is lit, the forehead usually is too; if two dark eyes sit near the middle, the bridge of the nose sits between them.
PCA starts by shifting the origin to the center of the cloud. Compute the mean image
and subtract it from every sample to form the centered data matrix , whose -th row is . From here on, every image is described solely by how it differs from the average face.
What PCA finds: one direction, two equivalent goals
Before jumping to dimensions, look at centered points in two dimensions. Suppose we must summarize each 2D point by a single number — its coordinate along some unit vector () — and reconstruct it as .
There are two natural ways to choose the best line :
- Keep as much spread as possible: maximize the variance of the projections,
- Lose as little information as possible: minimize the mean squared distance from each point to its projection on the line,
Rotate the direction on the plot below — by dragging the handle on the canvas or moving the slider — and watch the two bars on the right.
A 2D scatter plot of 64 centered points with a rotatable unit direction u(θ), perpendicular residual lines to each projection, and a split bar showing that projected variance plus residual MSE stays constant at every angle.
The sum of the two bars never moves: for this cloud it stays fixed at at every angle. The reason is the Pythagorean theorem. Because the residual is perpendicular to the projection ,
Averaging over all points gives a constant left-hand side (the total variance of the cloud). Therefore, maximizing projected variance and minimizing squared reconstruction error are the exact same problem. On the default cloud, both hit their optimum at , where PC1 captures of the variance and PC2 (perpendicular to it) takes the remaining . Lower the correlation slider towards zero and the cloud becomes round: every direction becomes equally good, and PCA has no preferred axis to exploit.
Covariance, eigenvectors, and SVD
Now write that optimization in matrix form for any dimension . The sample covariance matrix of the centered data is
The projected variance along any unit vector is the quadratic form
To maximize subject to , introduce a Lagrange multiplier and differentiate with respect to :
The stationary directions are the eigenvectors of the covariance matrix , and the variance along each eigenvector is its eigenvalue: . Because is symmetric and positive semidefinite, all eigenvalues are real and non-negative (), and the eigenvectors form an orthonormal basis of .
- (largest ) is the first principal component (
PC1). - maximizes variance among all directions orthogonal to , and so on.
- Keeping the top eigenvectors gives the best rank- linear approximation of the dataset in squared error (the Eckart–Young–Mirsky theorem).
In practice, you rarely form explicitly. For our faces of pixels, is a matrix, and squaring squares its condition number. Instead, compute the thin Singular Value Decomposition (SVD) of the centered matrix :
Plug this into :
The columns of are immediately the eigenvectors (the eigenfaces), the eigenvalues are where are the singular values in , and the coordinates of every training image in the eigenface basis are simply . When , the thin SVD costs instead of — on this dataset, that is hundreds of times less work.
How many numbers does a face take?
Because the eigenfaces are orthonormal, projecting any face onto the top components and rebuilding it is just
Slide from to below — or drag directly across the cumulative variance curve on the right — to watch a real face or dog emerge from the mean image.
Side-by-side original and k-component PCA reconstruction of a real 96×96 color face or dog next to the cumulative explained variance curve; moving k from 0 to 120 shrinks the reconstruction error as variance climbs.
The curve climbs steeply at first and then flattens into a long tail:
- On the human faces,
PC1alone carries of the variance andPC2adds another . - At , the reconstruction is just the mean face (RMSE on subject #1).
- At , the basis captures of the variance and RMSE drops to .
- At — storing 120 weights instead of RGB values, a compression — variance reaches and RMSE falls to . By it reaches .
Switch to Dogs and compare the curve: at 8 components, at 48, and at 120. Dog breeds vary far more in ear shape, fur markings, and snout geometry than human faces, so their spectrum has a heavier tail and takes more components to reach the same variance share.
Where linear PCA breaks down
Eigenfaces were a landmark in 1991 (Turk and Pentland), and they remain one of the clearest ways to see what a linear basis can and cannot do:
- Alignment is fragile. PCA treats pixel as an independent coordinate from pixel . Translate a face five pixels to the right and its projection onto the eigenfaces collapses, even though a human sees the exact same person.
- Appearance is nonlinear. Lighting on a fixed face is close to linear (adding two light sources adds pixel intensities), which is why
PC1andPC2capture illumination so cleanly. Turning a head in 3D, opening a mouth, or changing breed geometry traces a curved manifold that linear combinations can only smear across. - Scale matters. PCA maximizes raw variance. On images, every pixel shares the same scale; on tabular data where one column is in millimeters and another in kilometers, the larger-unit column dominates
PC1unless you standardize features first.
That is why modern vision models replaced raw-pixel PCA with convolutional and attention layers that handle translation and nonlinear geometry directly — while PCA itself remains the workhorse whenever you want an exact, closed-form, interpretable low-dimensional coordinate system.