A 96×9696 \times 96 color image is a list of 96×96×3=2764896 \times 96 \times 3 = 27648 numbers. Yet you do not need 2764827648 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 50005000 real human faces (CelebA-HQ) and 45004500 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:

x^=μ+∑i=1k(wiσi) vi.\hat{x} = \mu + \sum_{i=1}^{k} (w_i \sigma_i)\, v_i .

Here μ\mu is the mean image (press mean to see it with every weight set to zero), each viv_i is a unit-length principal component (an eigenface or eigendog), σi\sigma_i is the standard deviation along that axis, and wi∈[−3,3]w_i \in [-3, 3] is the slider value in units of σi\sigma_i. Clicking #1–#4 loads a real portrait's top 120120 coordinates so fine detail stays sharp while the eight sliders move PC1–PC8 live; clicking mean zeroes all 120120 weights so you can build an image purely from the first eight axes.

With only eight numbers out of 2764827648, those eight axes already account for 60.5%60.5\% of the total RGB variance across all 50005000 human faces (and 52.1%52.1\% across the 45004500 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 96×96×396 \times 96 \times 3 RGB channels of an image end to end and you get a single vector x∈Rdx \in \mathbb{R}^{d} with d=27648d = 27648. A dataset of nn images is a cloud of nn points in Rd\mathbb{R}^{d}, stored as rows of a matrix of shape n×dn \times d.

Almost the entire volume of Rd\mathbb{R}^{d} 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

μ=1n∑j=1nxj,\mu = \frac{1}{n} \sum_{j=1}^{n} x_j ,

and subtract it from every sample to form the centered data matrix X∈Rn×dX \in \mathbb{R}^{n \times d}, whose jj-th row is xj−μx_j - \mu. 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 2764827648 dimensions, look at 6464 centered points in two dimensions. Suppose we must summarize each 2D point xjx_j by a single number — its coordinate zj=xj⋅uz_j = x_j \cdot u along some unit vector uu (∥u∥=1\|u\| = 1) — and reconstruct it as x^j=(xj⋅u) u\hat{x}_j = (x_j \cdot u)\,u.

There are two natural ways to choose the best line uu:

  1. Keep as much spread as possible: maximize the variance of the projections, Var⁡(Xu)=1n−1∑j=1n(xj⋅u)2.\operatorname{Var}(X u) = \frac{1}{n-1} \sum_{j=1}^{n} (x_j \cdot u)^2 .
  2. Lose as little information as possible: minimize the mean squared distance from each point to its projection on the line, MSE⁡(u)=1n−1∑j=1n∥xj−(xj⋅u) u∥2.\operatorname{MSE}(u) = \frac{1}{n-1} \sum_{j=1}^{n} \bigl\| x_j - (x_j \cdot u)\,u \bigr\|^2 .

Rotate the direction u(θ)u(\theta) on the plot below — by dragging the handle on the canvas or moving the θ\theta 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 1.681.68 at every angle. The reason is the Pythagorean theorem. Because the residual xj−(xj⋅u) ux_j - (x_j \cdot u)\,u is perpendicular to the projection (xj⋅u) u(x_j \cdot u)\,u,

∥xj∥2=(xj⋅u)2+∥xj−(xj⋅u) u∥2.\|x_j\|^2 = (x_j \cdot u)^2 + \bigl\|x_j - (x_j \cdot u)\,u\bigr\|^2 .

Averaging over all nn 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 θ=33.0∘\theta = 33.0^\circ, where PC1 captures 93.0%93.0\% of the variance and PC2 (perpendicular to it) takes the remaining 7.0%7.0\%. 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 dd. The sample covariance matrix of the centered data X∈Rn×dX \in \mathbb{R}^{n \times d} is

C=1n−1X⊤X∈Rd×d.C = \frac{1}{n-1} X^\top X \in \mathbb{R}^{d \times d} .

The projected variance along any unit vector u∈Rdu \in \mathbb{R}^{d} is the quadratic form

1n−1∥Xu∥2=1n−1u⊤X⊤Xu=u⊤Cu.\frac{1}{n-1} \|X u\|^2 = \frac{1}{n-1} u^\top X^\top X u = u^\top C u .

To maximize u⊤Cuu^\top C u subject to u⊤u=1u^\top u = 1, introduce a Lagrange multiplier λ\lambda and differentiate L(u,λ)=u⊤Cu−λ(u⊤u−1)\mathcal{L}(u, \lambda) = u^\top C u - \lambda(u^\top u - 1) with respect to uu:

∇uL=2Cu−2λu=0  ⟹  Cu=λu.\nabla_u \mathcal{L} = 2 C u - 2 \lambda u = 0 \implies C u = \lambda u .

The stationary directions are the eigenvectors of the covariance matrix CC, and the variance along each eigenvector is its eigenvalue: u⊤Cu=u⊤(λu)=λu^\top C u = u^\top (\lambda u) = \lambda. Because CC is symmetric and positive semidefinite, all eigenvalues are real and non-negative (λ1≥λ2≥⋯≥λd≥0\lambda_1 \ge \lambda_2 \ge \dots \ge \lambda_d \ge 0), and the eigenvectors v1,…,vdv_1, \dots, v_d form an orthonormal basis of Rd\mathbb{R}^d.

  • v1v_1 (largest λ1\lambda_1) is the first principal component (PC1).
  • v2v_2 maximizes variance among all directions orthogonal to v1v_1, and so on.
  • Keeping the top kk eigenvectors gives the best rank-kk linear approximation of the dataset in squared error (the Eckart–Young–Mirsky theorem).

In practice, you rarely form C=1n−1X⊤XC = \frac{1}{n-1} X^\top X explicitly. For our 50005000 faces of 2764827648 pixels, CC is a 27648×2764827648 \times 27648 matrix, and squaring XX squares its condition number. Instead, compute the thin Singular Value Decomposition (SVD) of the centered matrix XX:

X=UΣV⊤.X = U \Sigma V^\top .

Plug this into CC:

C=1n−1VΣ⊤U⊤UΣV⊤=V(Σ2n−1)V⊤.C = \frac{1}{n-1} V \Sigma^\top U^\top U \Sigma V^\top = V \left(\frac{\Sigma^2}{n-1}\right) V^\top .

The columns of VV are immediately the eigenvectors (the eigenfaces), the eigenvalues are λi=σi2/(n−1)\lambda_i = \sigma_i^2 / (n-1) where σi\sigma_i are the singular values in Σ\Sigma, and the coordinates of every training image in the eigenface basis are simply XV=UΣX V = U \Sigma. When n≪dn \ll d, the thin SVD costs O(n2d)O(n^2 d) instead of O(d3)O(d^3) — 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 xx onto the top kk components and rebuilding it is just

x^(k)=μ+∑i=1k((x−μ)⋅vi) vi.\hat{x}^{(k)} = \mu + \sum_{i=1}^{k} \bigl((x - \mu) \cdot v_i\bigr)\, v_i .

Slide kk from 00 to 120120 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, PC1 alone carries 22.6%22.6\% of the variance and PC2 adds another 10.4%10.4\%.
  • At k=0k = 0, the reconstruction is just the mean face μ\mu (RMSE 0.2090.209 on subject #1).
  • At k=6k = 6, the basis captures 56.0%56.0\% of the variance and RMSE drops to 0.1150.115.
  • At k=120k = 120 — storing 120 weights instead of 2764827648 RGB values, a 230×230\times compression — variance reaches 88.5%88.5\% and RMSE falls to 0.0640.064. By k=200k = 200 it reaches 91.5%91.5\%.

Switch to Dogs and compare the curve: 52.1%52.1\% at 8 components, 77.3%77.3\% at 48, and 86.3%86.3\% 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 (12,20)(12, 20) as an independent coordinate from pixel (12,21)(12, 21). 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 PC1 and PC2 capture 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 [0,1][0, 1] scale; on tabular data where one column is in millimeters and another in kilometers, the larger-unit column dominates PC1 unless 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.