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 on data that never moved.
Step 4 says whose mean and standard deviation to use. “We need to standardize using the mean and standard deviation of the training data.” Using the test set’s own statistics instead makes the reported error smaller, which is why the mistake survives.
What you’ll learn
Section titled “What you’ll learn”- 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 swing.
- What Step 2 actually does: standardising then computing the covariance gives the correlation matrix, measured to , and pins the total variance at exactly .
- Measured on height and weight: unstandardised, the leading direction is in metres and in millimetres — apart. Standardised, all three unit choices agree to degrees.
- Equations 10.58–10.61, the forward and inverse maps, with a round trip measured at when .
- Two silent failures: dividing by a zero standard deviation produces NaNs on the digit-”8” images, and using the test set’s own statistics reports where the honest figure is .
- 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 , 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.
flowchart TD A["1. subtract the mean
page 1001: 87.9996 degrees if you skip it"] A --> B["2. divide by the sd
77.3982 degrees if you skip it
NaN if any sd is 0"] B --> C["3. eigendecompose S
page 1005: SVD, not eigh, if kappa is large"] C --> D["4. project: z = B'x
with the TRAINING mu and sigma"] D --> E["Eq 10.61: undo the standardisation
only if you want pictures"] D --> F["PCA returns z, not x~"]
§10.6 The six steps
Section titled “§10.6 The six steps”1. Mean subtraction. “This ensures that the dataset has mean . Mean subtraction is not strictly necessary but reduces the risk of numerical problems.”
2. Standardization. Divide by the standard deviation in every dimension. “Now the data is unit free, and it has variance 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 :
PCA returns the coordinates (10.60), not the projections .
5. Undoing the standardization, if you want the picture back in the original space:
Step 2 is a modelling decision
Section titled “Step 2 is a modelling decision”Take two genuinely different quantities — height and weight — with a correlation of , and vary nothing but the unit of the first column:
| units of height | leading direction | share of variance it keeps |
|---|---|---|
| metres | ||
| centimetres | ||
| millimetres |
| angle between two unstandardised answers | degrees |
|---|---|
| metres vs centimetres | |
| centimetres vs millimetres | |
| metres vs millimetres |
Standardise first, and all three become keeping — agreeing to degrees.
Two silent failures
Section titled “Two silent failures”A zero standard deviation
Section titled “A zero standard deviation”Equation 10.58 divides by . On the digit-”8” images, page 1001 measured pixels with variance exactly zero:
| value | |
|---|---|
| pixels with | of |
| NaNs produced by Equation 10.58 | () |
| infinities produced |
Every NaN is , 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.
The wrong mean and standard deviation
Section titled “The wrong mean and standard deviation”Step 4 is explicit that and come from the training data. Measured on a split of the digit-”8” images at :
| standardising the test set with | test RMS error, original units |
|---|---|
| the training mean and sd — Step 4 as written | |
| the test set’s own mean and sd |
The round trip
Section titled “The round trip”Equations 10.58 → 10.60 → 10.59 → 10.61, on the same split, reporting the error back in the original pixel units:
| test RMS reconstruction error | |
|---|---|
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, .)
-
Why does Step 2 matter beyond being tidy?
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.
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.
-
What matrix does the standardised data's covariance equal?
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.
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.
-
What happens if a dimension has zero variance and you apply Equation 10.58?
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.
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.
-
Step 4 says to standardise a new point with the training mean and standard deviation. What goes wrong otherwise?
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.
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.
Exercises
Section titled “Exercises”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 3 – Dividing by zero, quietly
Section titled “Exercise 3 – Dividing by zero, quietly”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”Recall card
Section titled “Recall card”- 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.coffeeCtapch.feedbackHeading
pch.feedbackSubheading