Skip to content

Key Steps of PCA in Practice

Six pages of derivation collapse into one recipe. §10.6 lists it, and two of its steps are not bookkeeping:

Step 2 changes the answer. The book calls it “standardization” and moves on. Measured below, skipping it lets a change of units rotate the leading direction by 77.398277.3982^\circ on data that never moved.

Step 4 says whose mean and standard deviation to use. “We need to standardize x\mathbf{x}_* using the mean μd\mu_d and standard deviation σd\sigma_d of the training data.” Using the test set’s own statistics instead makes the reported error smaller, which is why the mistake survives.

  • The six steps as Figure 10.11 draws them, run end to end on one dataset.
  • Why Step 1’s “not strictly necessary but reduces the risk of numerical problems” understates it — page 1001 already measured the 87.999687.9996^\circ swing.
  • What Step 2 actually does: standardising then computing the covariance gives the correlation matrix, measured to 6.7×10166.7\times10^{-16}, and pins the total variance at exactly DD.
  • Measured on height and weight: unstandardised, the leading direction is [0.003887,0.999992][0.003887, 0.999992] in metres and [0.976750,0.214380][0.976750, 0.214380] in millimetres — 77.398277.3982^\circ apart. Standardised, all three unit choices agree to 1.2×1061.2\times10^{-6} degrees.
  • Equations 10.58–10.61, the forward and inverse maps, with a round trip measured at 2.3×10142.3\times10^{-14} when M=DM = D.
  • Two silent failures: dividing by a zero standard deviation produces 2,0882{,}088 NaNs on the digit-”8” images, and using the test set’s own statistics reports 15.40025015.400250 where the honest figure is 15.78199215.781992.
  • The sentence at the end of Step 4: “PCA returns the coordinates (10.60), not the projections.”

Intuition: PCA has no idea what your columns mean

Section titled “Intuition: PCA has no idea what your columns mean”

Everything from §10.2 onward maximises variance, and variance has units. A column measured in millimetres has a variance a million times larger than the same column in metres — so an unstandardised PCA will point at whichever column happens to be recorded in the smallest unit.

Standardising sets every column’s variance to 11, which makes the question “which direction varies most” meaningful across columns that measure different things. It is a decision, not a correction: you are declaring that a one-standard-deviation move is equally important in every variable.

diagram The six steps, and where each one can go wrong mermaid

1. Mean subtraction. “This ensures that the dataset has mean 0\mathbf{0}. Mean subtraction is not strictly necessary but reduces the risk of numerical problems.”

2. Standardization. Divide by the standard deviation σd\sigma_d in every dimension. “Now the data is unit free, and it has variance 11 along each axis.”

3. Eigendecomposition of the covariance matrix. By the spectral theorem an ONB of eigenvectors exists; §10.4 covered how to obtain it.

4. Projection. For any x\mathbf{x}_*:

x(d)x(d)μdσd,d=1,,D(10.58)x_*^{(d)} \leftarrow \frac{x_*^{(d)} - \mu_d}{\sigma_d}, \qquad d = 1,\ldots,D \qquad \text{(10.58)} x~=BBx,z=Bx(10.59), (10.60)\tilde{\mathbf{x}}_* = \mathbf{B}\mathbf{B}^\top\mathbf{x}_*, \qquad \mathbf{z}_* = \mathbf{B}^\top\mathbf{x}_* \qquad \text{(10.59), (10.60)}

PCA returns the coordinates (10.60), not the projections x\mathbf{x}_*.

5. Undoing the standardization, if you want the picture back in the original space:

x~(d)x~(d)σd+μd,d=1,,D(10.61)\tilde{x}_*^{(d)} \leftarrow \tilde{x}_*^{(d)}\sigma_d + \mu_d, \qquad d = 1,\ldots,D \qquad \text{(10.61)}
figure Figure 10.11, run end to end matplotlib
Six scatter plots. The first shows a tilted cloud away from the origin; the second the same cloud centred; the third the same rescaled to unit variance with two axis arrows; the fourth the same with two eigenvector arrows of different lengths; the fifth the points collapsed onto a line; the sixth that line mapped back over the original cloud. Six scatter plots. The first shows a tilted cloud away from the origin; the second the same cloud centred; the third the same rescaled to unit variance with two axis arrows; the fourth the same with two eigenvector arrows of different lengths; the fifth the points collapsed onto a line; the sixth that line mapped back over the original cloud.
Panels (c) and (d) hold the same points and differ only in what is drawn on them: the unit-variance axes Step 2 creates, then the eigenvectors Step 3 finds, scaled by the square roots of their eigenvalues. Panel (f) is Equation 10.61 — the projection carried back through the standardisation, which is why the orange line in (f) is not at 45 degrees.

Take two genuinely different quantities — height and weight — with a correlation of 0.91980.9198, and vary nothing but the unit of the first column:

units of heightleading direction b1\mathbf{b}_1share of variance it keeps
metres[0.003887, 0.999992][0.003887,\ 0.999992]100.00%100.00\%
centimetres[0.369903, 0.929070][0.369903,\ 0.929070]97.98%97.98\%
millimetres[0.976750, 0.214380][0.976750,\ 0.214380]99.22%99.22\%
angle between two unstandardised answersdegrees
metres vs centimetres21.4921.49
centimetres vs millimetres55.9155.91
metres vs millimetres77.3982\mathbf{77.3982}

Standardise first, and all three become [0.707107, 0.707107][0.707107,\ 0.707107] keeping 95.99%95.99\% — agreeing to 1.2×1061.2\times10^{-6} degrees.

figure The same data, three units, three different first principal components matplotlib
Top, three scatter plots of the same data with a red line through each; the line is nearly vertical in the first, diagonal in the second and nearly horizontal in the third. Bottom left, a log-scale bar chart pairing three large red bars around twenty to eighty degrees with three blue bars near 1e-7. Bottom right, two bars, 386.4299 and 2.0000. Top, three scatter plots of the same data with a red line through each; the line is nearly vertical in the first, diagonal in the second and nearly horizontal in the third. Bottom left, a log-scale bar chart pairing three large red bars around twenty to eighty degrees with three blue bars near 1e-7. Bottom right, two bars, 386.4299 and 2.0000.
Nothing about the data changed between the top three panels — only the number written on the height axis. Unstandardised PCA reports whichever column happens to carry the largest numbers. After Step 2 the three answers are the same direction to a millionth of a degree, and the total variance is exactly D.

Equation 10.58 divides by σd\sigma_d. On the digit-”8” images, page 1001 measured 1212 pixels with variance exactly zero:

value
pixels with σd=0\sigma_d = 01212 of 6464
NaNs produced by Equation 10.582,088\mathbf{2{,}088} (=12×174= 12 \times 174)
infinities produced00

Every NaN is 0/00/0, not a division of a nonzero number. np.linalg.eigh on the result raises nothing useful; replacing the NaNs with zeros runs and decomposes a matrix built from values you invented. The fix is to drop the constant dimensions first — they carry no information by construction, so removing them costs nothing.

figure Both of these fail without raising anything matplotlib
Left, an eight-by-eight heat map of pixel standard deviations with twelve dark cells around the left and right borders outlined in blue. Right, two nearly overlapping curves of test error against M, the red one slightly below the blue throughout. Left, an eight-by-eight heat map of pixel standard deviations with twelve dark cells around the left and right borders outlined in blue. Right, two nearly overlapping curves of test error against M, the red one slightly below the blue throughout.
Left: the outlined border pixels are black in every image, so Equation 10.58 divides zero by zero for each of them. Right: standardising the test set with its own mean and standard deviation reports a lower error at every M — 15.400250 against 15.781992 at M = 10 — because it has quietly re-centred the test set in a frame the basis was never fitted in.

Step 4 is explicit that μd\mu_d and σd\sigma_d come from the training data. Measured on a 140/34140/34 split of the digit-”8” images at M=10M = 10:

standardising the test set withtest RMS error, original units
the training mean and sd — Step 4 as written15.78199215.781992
the test set’s own mean and sd15.400250\mathbf{15.400250}

Equations 10.58 → 10.60 → 10.59 → 10.61, on the same split, reporting the error back in the original pixel units:

MMtest RMS reconstruction error
1124.17925224.179252
5519.65818919.658189
101015.78199215.781992
202011.74825511.748255
40406.4731576.473157
64643.3×1014\mathbf{3.3\times10^{-14}}

The last row is the check that matters: with every component kept, the whole pipeline — centre, standardise, project, unproject, unstandardise — is the identity to floating point. (The exact figure drifts in the last couple of digits between runs; what matters is that it is at machine level and not, say, 10610^{-6}.)

pch.quizTag Is the recipe clear?
  1. Why does Step 2 matter beyond being tidy?

    pch.quizShowAnswer

    B — Without it the answer depends on the units — measured, a metres-to-millimetres change rotates the leading direction 77.3982 degrees — Variance has units, so a column recorded in a smaller unit has a larger variance and attracts the leading direction. In metres it comes out as [0.003887, 0.999992] and in millimetres as [0.976750, 0.214380] — the same data both times. After Step 2 the three unit choices agree to 1.2e-06 degrees.

  2. What matrix does the standardised data's covariance equal?

    pch.quizShowAnswer

    B — The correlation matrix, and its trace is exactly D for any dataset — Measured to 6.7e-16. That is why the fraction-of-variance figure only means something after Step 2: the raw trace on height and weight was 386.429897 in units of squared metres plus squared kilograms, which is not a quantity. The cost is that you have declared a one-standard-deviation move equally important in every column.

  3. What happens if a dimension has zero variance and you apply Equation 10.58?

    pch.quizShowAnswer

    B — You divide zero by zero — 2,088 NaNs on the digit-8 images, with no error raised — Twelve of the sixty-four pixels are black in every image, giving 12 times 174 NaNs. No exception is raised; replacing them with zeros produces a decomposition of numbers you invented. Drop the constant dimensions first — they carry no information by construction.

  4. Step 4 says to standardise a new point with the training mean and standard deviation. What goes wrong otherwise?

    pch.quizShowAnswer

    B — The reported error goes DOWN — 15.400250 against an honest 15.781992 — and stops measuring anything — That is what makes the mistake durable: it flatters the result. The basis was fitted in the training coordinate frame, and re-standardising the test set moves it into a different frame that fits its own spread slightly better. The training and test means here differ by 5.900273.

Exercise 1 – Change the unit, change the answer

Section titled “Exercise 1 – Change the unit, change the answer”

Exercise 2 – What Step 2 turns the covariance into

Section titled “Exercise 2 – What Step 2 turns the covariance into”

Exercise 4 – The round trip, in the original units

Section titled “Exercise 4 – The round trip, in the original units”

Exercise 5 – Whose mean and standard deviation

Section titled “Exercise 5 – Whose mean and standard deviation”
  • Section 10.6 is the recipe: centre, standardise, eigendecompose, project with the training statistics, and optionally undo the standardisation to get a picture back.
  • Step 2 is a modelling decision, not tidiness. Variance carries the square of whatever unit you wrote down, so an unstandardised PCA points at whichever column has the largest numbers.
  • Measured, switching height from metres to millimetres rotates the leading direction 77.3982 degrees on data that never moved. After standardising, three unit choices agree to about a millionth of a degree.
  • Standardising and then computing Equation 10.1 gives the correlation matrix, matched to 6.7e-16, and its trace is exactly D for every dataset.
  • Which is why a percentage of variance explained only means something after Step 2 — the raw trace on height and weight was a sum of squared metres and squared kilograms.
  • But the trade-off is real: when the columns share a unit, the fact that one varies more is information, and standardising throws it away. The digit pages in this chapter do not standardise.
  • A zero standard deviation makes Equation 10.58 compute zero over zero. On the digit-8 images that is 2,088 NaNs and no exception. Drop the constant columns first.
  • Step 4 says to use the training mean and standard deviation for any new point, and the failure mode is the dangerous kind: using the test set’s own statistics reports 15.400250 where the honest number is 15.781992.
  • A leak that flatters the result is the one that survives code review.
  • Kept in full, the pipeline is the identity to about 3e-14 — the check worth writing whenever you implement it.
  • PCA returns the coordinates, not the projections. The code is M numbers; the reconstruction is D numbers with only M degrees of freedom, and exists so you can look at it.

Next: The Latent Variable Perspective — §10.7 puts a probabilistic model behind all of this, and recovers everything so far as the noise-free limit.

pch.coffeeTagline

pch.coffeeCta

pch.feedbackHeading

pch.feedbackSubheading