Principal Component Analysis

Find the directions your data spreads along. Spin a line to the best fit, watch power iteration find it, see why scaling matters and rebuild digits from a few components.

PC1PC2

1Find the direction of most spread

PCA looks for the line through the middle of the data along which the points spread out the most. That's the first principal component. The second is at right angles to it.

2 numbers → 1, little lost

2Drop every point onto it

Projecting onto PC1 turns each point (two numbers) into one: how far along the line it lands. The short drops are what you lose, and PC1 is the line that makes them smallest.

PC1 … PC9running total3 components keep 87%

3Keep the few that matter

Real data has many features, but often a handful of components hold most of the variation. Sort them by variance and keep the top few: smaller data, less noise, and something you can plot.

Start here

Spin the line

Forty people's height and arm span. The two go together, so the points lie along a slanted band. Suppose you could keep only one number per person. Pick a line through the average (the cross) and drop every point onto it: the amber shadows are what you keep, the red drops are what you lose. Turn the line and find the angle that keeps the most.

150160170180190150160170180190162.5 cm tall, 161.5 cm span178.5 cm tall, 175.5 cm span172.5 cm tall, 172.6 cm span164.7 cm tall, 167.8 cm span165.6 cm tall, 174.1 cm span172.2 cm tall, 168.7 cm span169.1 cm tall, 176.4 cm span160.6 cm tall, 160.8 cm span167.9 cm tall, 164.3 cm span178.6 cm tall, 180.2 cm span175.9 cm tall, 179.9 cm span179.4 cm tall, 185.1 cm span170.4 cm tall, 172.7 cm span172.5 cm tall, 177.2 cm span173.2 cm tall, 175.2 cm span171.1 cm tall, 175.8 cm span175.8 cm tall, 176.9 cm span177.4 cm tall, 179 cm span168.2 cm tall, 168.1 cm span165 cm tall, 166.6 cm span175.4 cm tall, 172.8 cm span168.4 cm tall, 164.3 cm span170.1 cm tall, 172.8 cm span177.7 cm tall, 184 cm span180 cm tall, 187.6 cm span183.6 cm tall, 181.4 cm span164.3 cm tall, 167.1 cm span160.3 cm tall, 158.7 cm span189.6 cm tall, 185.6 cm span160.1 cm tall, 155.3 cm span177.2 cm tall, 181.4 cm span179.2 cm tall, 180.8 cm span182.2 cm tall, 178.2 cm span164.8 cm tall, 161.4 cm span169 cm tall, 171.5 cm span179.2 cm tall, 182.5 cm span153.6 cm tall, 146.9 cm span167.5 cm tall, 171.4 cm span167.5 cm tall, 165.5 cm span172 cm tall, 168 cm span
a person (height across, span up, in cm)their shadow on the linewhat's lost
10°
Spread kept (cm²)
77.5
Lost (cm²)
59.0

57% kept of a total 136.5 cm², which never changes

Keep turning: the best angle makes the red drops as short as possible.

04590135180050100PC1
spread kept along the linelost across it

x: the line's angle in degrees

Two ways to say the same thing

Each point's distance from the average splits into a part along the line and a part across it, and by Pythagoras along² + across² is fixed. So the line that keeps the most spread is also the one that loses the least. PCA is both: maximum variance, and minimum reconstruction error.

How a computer finds it

Step through PCA

Turning a slider works in 2-D, not with hundreds of features. The real recipe: centre the data, build the covariance matrix, and find its eigenvectors. Power iteration is the simplest way: start with any arrow and keep multiplying it by the covariance matrix. Each multiplication stretches it most along the direction of most spread, so the arrow swings round to PC1.

150160170180190150160170180190162.5 cm tall, 161.5 cm span178.5 cm tall, 175.5 cm span172.5 cm tall, 172.6 cm span164.7 cm tall, 167.8 cm span165.6 cm tall, 174.1 cm span172.2 cm tall, 168.7 cm span169.1 cm tall, 176.4 cm span160.6 cm tall, 160.8 cm span167.9 cm tall, 164.3 cm span178.6 cm tall, 180.2 cm span175.9 cm tall, 179.9 cm span179.4 cm tall, 185.1 cm span170.4 cm tall, 172.7 cm span172.5 cm tall, 177.2 cm span173.2 cm tall, 175.2 cm span171.1 cm tall, 175.8 cm span175.8 cm tall, 176.9 cm span177.4 cm tall, 179 cm span168.2 cm tall, 168.1 cm span165 cm tall, 166.6 cm span175.4 cm tall, 172.8 cm span168.4 cm tall, 164.3 cm span170.1 cm tall, 172.8 cm span177.7 cm tall, 184 cm span180 cm tall, 187.6 cm span183.6 cm tall, 181.4 cm span164.3 cm tall, 167.1 cm span160.3 cm tall, 158.7 cm span189.6 cm tall, 185.6 cm span160.1 cm tall, 155.3 cm span177.2 cm tall, 181.4 cm span179.2 cm tall, 180.8 cm span182.2 cm tall, 178.2 cm span164.8 cm tall, 161.4 cm span169 cm tall, 171.5 cm span179.2 cm tall, 182.5 cm span153.6 cm tall, 146.9 cm span167.5 cm tall, 171.4 cm span167.5 cm tall, 165.5 cm span172 cm tall, 168 cm span
power iteration's arrowPC1PC2

Covariance matrix (cm²)

Not built yet.

Step 0 / 11
900 ms

Press Start to run PCA on the height and arm-span data, one step at a time.

Six features, two that matter

Scale your features first

120 students, six results: Maths, Physics, Chemistry, English and History out of 100, plus reading pace in words per minute. Underneath, three things drive them: being a strong student overall, a knack for science, a knack for humanities. Can PCA find that? Only if the features are on the same scale.

-60-3003060-60-3003060PC1: mostly reading →↑ PC2: more science
better at sciencebetter at humanities
Features

PC1 weights (82% of the variance)

  • Maths0.08
  • Physics0.06
  • Chemistry0.06
  • English0.18
  • History0.20
  • Reading0.96

PC2 weights (13%)

  • Maths0.56
  • Physics0.63
  • Chemistry0.53
  • English-0.01
  • History-0.00
  • Reading-0.12

Raw, reading pace varies by dozens of words a minute while marks vary by a few points, so it has by far the biggest variance and PC1 is almost pure reading. The structure we wanted is buried.

0%25%50%75%100%PC1PC2PC3PC4PC5PC6
the two components plotted aboverunning total: 95% after two

This is a scree plot. Keep components up to the “elbow”, or until the running total passes a target such as 90%. Standardised PCA is the same as PCA on the correlation matrix.

Hundreds of features

Rebuild a digit from a few numbers

Each picture is 16 × 16 = 256 pixels, so 256 features. PCA on 300 drawn digits finds the components; each one is itself a picture, a pattern of ink to add or remove. Store just the first few weights per digit and rebuild it from them. Slide to see how few you need.

Fitting PCA to 300 digits…

Handwritten digits are also the classic first dataset for convolutional neural networks (CNNs), but the two do different jobs. PCA only compresses: it squeezes each picture into a few numbers and rebuilds it, without ever learning which digit it is. A CNN learns to recognise the digit, by finding strokes and shapes wherever they appear in the picture. A common pipeline does both: PCA first to shrink the pixels, then a classifier on the few numbers that are left.

Know its limits

The catch, and where PCA is used

Straight lines only

PCA finds flat directions. Data on a curved surface (a rolled-up sheet, a circle) gets squashed badly. Non-linear methods like t-SNE, UMAP or autoencoders handle curves.

Variance isn't usefulness

PCA never looks at labels. The direction that separates two classes can have little variance and be the first one thrown away. LDA, its supervised cousin, looks for separation instead.

Hard to read, easy to pull

Each component mixes every feature, so it can be hard to explain. And variance is about squares, so a few outliers can drag a component toward themselves.

Where you'll meet it

Plotting high-dimensional data, such as embeddings, in 2-D; shrinking features before KNN or a model; removing noise; and as the first look at any new dataset. Embeddings →

In practice libraries compute PCA with the singular value decomposition (SVD) of the centred data instead of building the covariance matrix: same components, more accurate. See also KNN and the curse of dimensionality →

No magic, just arithmetic

The maths, step by step

Four points: (2, 2), (3, 5), (5, 4), (6, 7). Find the first principal component by hand, then squash one point down to a single number and rebuild it.

  1. 1

    Centre the data

    Subtract the mean of each feature, so the cloud sits around the origin.

    mean x = (2 + 3 + 5 + 6) ÷ 4 = 4.0000

    mean y = (2 + 5 + 4 + 7) ÷ 4 = 4.5000

    centred: (-2, -2.5), (-1, 0.5), (1, -0.5), (2, 2.5)

  2. 2

    Build the covariance matrix

    Average products of the centred values (dividing by n − 1 = 3). The diagonal is each feature's variance; off it, how they move together.

    var(x) = ((-2)² + (-1)² + 1² + 2²) ÷ 3 = 3.3333

    var(y) = ((-2.5)² + 0.5² + (-0.5)² + 2.5²) ÷ 3 = 4.3333

    cov(x, y) = ((-2)·(-2.5) + (-1)·0.5 + 1·(-0.5) + 2·2.5) ÷ 3 = 3.0000

    C = [[3.333, 3.000], [3.000, 4.333]]

  3. 3

    Its eigenvalues

    For a 2 × 2 symmetric matrix [[a, b], [b, c]] there's a formula: λ = (a + c)/2 ± √(((a − c)/2)² + b²). Each λ is the variance along one principal direction.

    (a + c)/2 = (3.333 + 4.333) ÷ 2 = 3.8333

    √(((a − c)/2)² + b²) = √(-0.500² + 3.000²) = 3.0414

    λ₁ = 3.8333 + 3.0414 = 6.8747

    λ₂ = 3.8333 − 3.0414 = 0.7920

  4. 4

    The first principal component

    The direction for λ₁: the vector (b, λ₁ − a) satisfies C v = λ₁ v. Scale it to length 1.

    (b, λ₁ − a) = (3.0000, 3.5414), length 4.6413

    PC1 x = 3.0000 ÷ 4.6413 = 0.6464

    PC1 y = 3.5414 ÷ 4.6413 = 0.7630

    check: C·PC1 = (4.4436, 5.2455) = 6.8747 × PC1 ✓

  5. 5

    How much does it keep?

    The eigenvalues add up to the total variance (the sum of the diagonal). PC1's share is its explained variance.

    explained by PC1 = 6.8747 ÷ (6.8747 + 0.7920) = 0.8967

  6. 6

    Squash a point to one number, and rebuild it

    The first point, (2, 2). Its score is the dot product of its centred value with PC1. To rebuild, walk that far along PC1 from the mean.

    score = (-2)·0.646 + (-2.5)·0.763 = -3.2003

    rebuilt x = 4 + (-3.2003)·0.6464 = 1.9314

    rebuilt y = 4.5 + (-3.2003)·0.7630 = 2.0581

    (1.93, 2.06) instead of (2, 2): close, from one number instead of two. The small miss is the part along PC2 that we threw away.

Keep going

Resources

Hand-picked places to go deeper. They're all free to read or watch, and they open in a new tab.

How it works: Principal Component Analysis

Step by step

  • Power iteration's arrow, and projected points
  • PC1
  • PC2

Press Start or Step: the step the animation is on lights up below.

  1. 1

    Centre the data

    Subtract each feature's mean (and divide by its standard deviation, if standardising) so the components pass through the middle of the cloud.

    Z = (X - mean) / std
  2. 2

    Build the covariance matrix

    A d × d matrix: each feature's variance on the diagonal, how pairs of features move together off it.

    C = Zᵀ Z / (n - 1)
  3. 3

    Power iteration

    Start from any vector and repeatedly multiply by C and rescale. It turns toward the eigenvector with the largest eigenvalue.

    v = C v / |C v| until it stops moving
  4. 4

    Record the component

    The settled vector is a principal component; vᵀCv is the variance along it. Deflate C (remove that direction) and iterate again for the next one.

    λ = vᵀ C v; C = C - λ v vᵀ
  5. 5

    Project

    Each point's new coordinates are its dot products with the kept components. Rebuild an approximation as mean + Σ score × component.

    scores = Z · [pc1 … pck]

Principal component analysis (PCA) finds new axes for your data: the direction along which the points spread out most (PC1), then the most-spread direction at right angles to it (PC2), and so on. Keep the first few and you have fewer features that still hold most of the variation.

The components are the eigenvectors of the covariance matrix, and each eigenvalue is the variance along its component. Its share of the total is the explained variance. Picking the line of most spread is the same as picking the line that loses least when you project onto it.

Centre the data first, and standardise it when the features are in different units, or the feature with the biggest numbers takes over. PCA is linear and unsupervised: it finds flat directions of variance and never looks at labels.

Where you'll meet it

  • Plotting data with many features (embeddings, gene expression, survey answers) in 2-D or 3-D to see its shape.
  • Shrinking features before another model: faster training, less noise, and less of the curse of dimensionality for distance-based methods such as KNN.
  • Interview favourites: “what are the principal components?”, “why centre and scale?”, “how many components do you keep?”, “how does PCA relate to SVD?”, “when would PCA fail?”.
time O(n · d² + d³)space O(d²)

Building the covariance matrix costs n · d², and a full eigendecomposition d³. Power iteration for the top k components costs about k · d² per iteration. Libraries use the SVD of the centred data, O(n · d · min(n, d)), or randomised SVD to get just the top k fast.