2.5.9 · D2Unsupervised Learning

Visual walkthrough — PCA via eigendecomposition and SVD

2,222 words10 min readBack to topic

We work with the tiniest possible example — a handful of dots on a flat page — because everything that happens in 1000 dimensions already happens in 2.


Step 1 — A cloud of dots, and the question we are asking

WHAT. We have some data: five points, each described by two numbers. Number one we call (say, hours studied), number two we call (say, exam score). Each point is a little arrow-tip on a flat sheet.

WHY. Before any math, we must know the question. Look at the cloud in the picture: it is not a round blob. It is stretched — long in one direction, thin in the other. PCA asks a single question:

Which single straight direction, if I had to keep only one, best captures how the data spreads out?

That "best direction" is what we will chase for the whole page.

PICTURE. The dots form a tilted sausage. The long axis of that sausage is the answer we want — but we do not know it yet, so it is drawn as a dashed guess.

Figure — PCA via eigendecomposition and SVD

Step 2 — Slide the cloud to the origin (centering)

WHAT. Compute the average point — the balance point of the cloud — and subtract it from every dot. The whole cloud slides so its center sits exactly on .

  • — the mean, i.e. average and average .
  • — a column of ones; multiplying it by just stamps that same mean into all rows so the subtraction lines up.

WHY. PCA measures spread, and spread is always measured from the center. If we forgot to center, the very first "direction of most spread" would just point from the origin toward the far-away cloud — a direction about where the cloud sits, not how it is shaped. Centering deletes the "where" so only the "shape" remains.

PICTURE. Same sausage, now straddling the crosshairs. Its tilt (the thing we care about) is untouched; only its position changed.

Figure — PCA via eigendecomposition and SVD

Step 3 — Score a candidate direction by projection

WHAT. Pick any unit-length arrow (a "candidate direction", length exactly 1). Drop each dot perpendicularly onto the line through . The shadow's signed distance from the origin is the projection:

  • — the dot product: it multiplies the two arrows and reads off "how much of lies along ". We use the dot product precisely because that is the one operation that answers "how far along this direction?".
  • — the angle between the dot and ; when the dot lines up with , and the shadow is longest.

WHY. A direction is "good" if the shadows spread out a lot — that means the direction runs along the data. If shadows all pile up near zero, the direction runs across the thin waist and is a poor summary.

PICTURE. One candidate arrow (yellow) with dotted drop-lines from each dot to its shadow (red ticks) on the -line.

Figure — PCA via eigendecomposition and SVD

Step 4 — Turn "spread of shadows" into one number: variance

WHAT. Collect all the shadow values and measure how spread out they are with the variance:

(The already have mean 0 because we centered in Step 2, so no "" is needed.)

  • — square each shadow so that far-from-center shadows on either side count as "spread", never cancelling.
  • — averaging, with Bessel's correction: we divide by instead of because one degree of freedom was already spent estimating the mean.

Now rewrite the sum using matrices. Since , stacking gives , and:

WHY. This is the magic compression: the entire cloud's spread along any direction is captured by one small object , the covariance matrix. We no longer need the raw dots — only .

PICTURE. A number line of the five shadow values with their spread bracketed, labelled .

Figure — PCA via eigendecomposition and SVD

Step 5 — The maximisation: why the winner is an eigenvector

WHAT. We want the that makes as large as possible, subject to . That constraint is handled with a Lagrange multiplier — a bookkeeping number that lets us optimise while obeying the length rule:

A constrained maximum requires both partial derivatives to vanish — one for , one for .

Derivative w.r.t. (the top of any hill is flat):

Derivative w.r.t. (this is what forces the length rule back on):

The second equation just re-states — it guarantees our winning direction is a genuine unit vector, closing the optimisation properly.

WHY the derivative? The best direction sits at the peak of the spread-versus-angle curve. At a peak the slope is zero — that is exactly what setting the derivative to zero finds. We use calculus here because "maximise a smooth quantity" is the one question derivatives were built to answer.

The boxed line is the eigenvalue equation: the best direction is an eigenvector of . And plugging it back (using from the second condition):

So the eigenvalue is the variance along its eigenvector.

PICTURE. A polar plot: spread as a function of the arrow's angle. The two peaks sit exactly where the arrow aligns with an eigenvector; the taller peak is .

Figure — PCA via eigendecomposition and SVD

Step 6 — All the directions at once, and every case covered

WHAT. is symmetric, so it has real eigenvectors that are mutually perpendicular, with eigenvalues sorted . These are the principal components: = biggest-spread direction, = biggest-spread direction among those perpendicular to , and so on.

WHY ? is positive semi-definite (it is a sum of squares), so no eigenvalue can be negative — you cannot have negative spread.

Every case:

  • Round blob (): no direction wins; PCA has no preferred axis (the polar plot is a circle). PCA still runs, but the axes are arbitrary.
  • Perfect line (): all spread on one axis, zero on the other. The second PC captures nothing — you can drop it losslessly.
  • A single point / all points identical: , all . Degenerate: there is no direction of spread at all.
  • One feature on a huge scale (e.g. metres vs millimetres): that feature fakes a large purely from units — this is why you scale first (2.4.03-Feature-Scalingand-Normalization).

PICTURE. The ellipse of the cloud with both eigenvectors drawn — long axis (blue), short axis (green), perpendicular, with the three degenerate ellipses shown as small insets.

Figure — PCA via eigendecomposition and SVD

Step 7 — Project, and the SVD shortcut

WHAT. To compress to dimensions, stack the top eigenvectors as columns of and take shadows:

is — the same points described by only numbers each.

The SVD shortcut. We never actually needed to build . Any centered matrix factors as (Linear-Algebra-SVD). Its right singular vectors are already the principal components, and:

  • — the -th singular value (how strongly the data stretches along ).
  • Squaring and dividing by converts "stretch of the data" into "variance". This matches Step 5's exactly.

WHY prefer SVD? Forming squares the numbers and can lose precision; SVD works on directly — more numerically stable, and faster when features samples.

PICTURE. The 2D cloud collapsing onto the line: dots turning into their 1D shadow values, "2 numbers → 1 number".

Figure — PCA via eigendecomposition and SVD

The one-picture summary

Everything above, in a single frame: raw cloud → centered → the covariance ellipse → its two eigen-axes → projection onto PC1.

Figure — PCA via eigendecomposition and SVD
Recall Feynman retelling — say it back in plain words

I had a cloud of dots that was long in one direction and thin in another. First I slid the cloud so its middle sat on the origin, because I only care about its shape, not where it is. Then, to test any direction, I dropped every dot's shadow onto a line and measured how spread out those shadows were — that "spread" is variance, and I found it equals a compact formula , where is one small matrix that remembers the whole cloud. I wanted the direction with the most spread, so I climbed to the top of the spread-hill by setting both derivatives (in and in ) to zero, and out popped with a unit vector — the best direction is an eigenvector, and its eigenvalue is its variance. The eigenvectors come out perpendicular and ranked, giving me my principal components. Finally I could either eigen-decompose or, more cleanly, run SVD straight on the data, where the right singular vectors are already the answer and .

Recall Quick self-test

Why must we center before PCA? ::: Variance is measured from the mean; without centering PC1 just points at the cloud's location instead of its shape. What does the eigenvalue physically mean? ::: The variance (spread) of the data along principal component . How do singular values relate to eigenvalues? ::: . What happens to PC2 when the cloud is a perfect line? ::: Its eigenvalue is 0 — it captures no variance and can be dropped losslessly. Does dividing by instead of change the principal directions? ::: No — only the eigenvalues rescale by ; the eigenvectors and variance ratios are unchanged.

Related: 2.5.01-Introduction-to-Unsupervised-Learning · 2.5.07-K-Means-Clustering · 2.6.04-Kernel-PCA · 3.2.05-Autoencoders (a nonlinear generalisation of this same compress-then-reconstruct idea).