2.5.9 · D4Unsupervised Learning

Exercises — PCA via eigendecomposition and SVD

2,898 words13 min readBack to topic

Before we start, a one-line reminder of the four moving parts, so no symbol below is a surprise:

One acronym recurs below — let us define it once, up front, so it is never a surprise:

The figure below shows the whole PCA idea we will keep exercising: a tilted data cloud, its center, and the two variance directions PCA extracts. In the picture, the arrow labelled "PC1 (max variance)" lies along the longest spread of the data points; the arrow labelled "PC2 (perp.)" is perpendicular and much shorter, and the point labelled "mean" marks the center we measure spread from. Every figure on this page carries its own legend and text labels — read those labels, not colors, so the reference is unambiguous. Every exercise on this page is really about finding, measuring, or reasoning about those two arrows.

Figure — PCA via eigendecomposition and SVD

L1 — Recognition

Exercise 1

The covariance matrix of a dataset is Without any calculation, name the two principal component directions and the variance each explains.

Recall Solution

What we notice: is already diagonal. A diagonal matrix has no coupling between features — feature 1 and feature 2 do not co-vary (off-diagonals are ).

Why that settles it: for a diagonal matrix the eigenvectors are just the coordinate axes and , and the eigenvalues are the diagonal entries themselves (check: ).

Answer:

  • PC1 = with variance .
  • PC2 = with variance .

Exercise 2

True or false: "You can skip centering the data because it does not change which directions have the most spread." Justify in one sentence.

Recall Solution

False. Variance is measured from the mean; if you do not center, measures spread from the origin instead. The first "component" then tends to point from the origin toward the data cloud's mean (a direction that carries no information about the shape of the cloud).


L2 — Application

Exercise 3

Given the centered data below (already centered — verify the means are ): Compute using in the denominator.

Recall Solution

Check centering: column sums are and . Good, means are .

Compute (a result): Divide by : The off-diagonal (equal to the diagonal) tells us the two features move together perfectly — the data lies on a line. The figure below plots these three points: they sit exactly on the line, which is why the second variance direction will collapse to zero.

Figure — PCA via eigendecomposition and SVD

Exercise 4

For from Exercise 3, find both eigenvalues and the eigenvector for the larger one.

Recall Solution

Set up the characteristic equation : So , .

Interpret : a direction with zero variance — the data does not spread there at all (it sits exactly on a line). This is a degenerate/rank-1 case, and it is normal, not an error.

Eigenvector for : solve : Pick (normalized to unit length). It points along the line — exactly where all three points sit (see the figure in Exercise 3: PC1 runs down that line, PC2 perpendicular to it captures nothing).

Exercise 5

Using , project the three rows of from Exercise 3 onto PC1.

Recall Solution

Projection is a dot product ; each row's projected value is (row)·:

  • Row : .
  • Row : .
  • Row : .

Answer: . One number now captures each point with zero loss (because ). Geometrically (Exercise 3 figure) these are just the signed distances along the line from the center.


L3 — Analysis

Exercise 6

A covariance matrix has eigenvalues . What is , and what fraction of variance is lost if you keep only the first component?

Recall Solution

Trace = sum of eigenvalues (a theorem: trace is basis-independent and equals ): .

Kept by PC1 (using the EVR defined at the top of the page): .

Lost: , i.e. of the total variance is discarded. Keeping one PC here is aggressive — you throw away nearly half the spread.

Exercise 7

You run PCA on 2D data and get . A classmate claims the second principal component is . Is that possible? What must be?

Recall Solution

The key property: the covariance matrix is symmetric, and eigenvectors of a symmetric matrix belonging to different eigenvalues are orthogonal (perpendicular). So PC2 must be perpendicular to PC1.

Test the claim with a dot product (dot product ⟺ perpendicular): Not perpendicular — the claim is wrong. In the figure below, read the text labels: the arrow labelled "claimed [0.6, 0.8]" is nowhere near a right angle from the arrow labelled "PC1", whereas the arrow labelled "correct PC2" meets PC1 at a square corner.

Correct PC2: rotate PC1 by , which swaps components and flips one sign: Check: . ✓ (A PC's sign is arbitrary — both are valid.)

Figure — PCA via eigendecomposition and SVD

Exercise 8

Data has samples. SVD of gives singular values , . Find the two eigenvalues of and the explained-variance ratio of PC1.

Recall Solution

Use the SVD–eigenvalue bridge (a singular value is a length, so squaring it turns it into a variance-like quantity, and dividing by matches the covariance's Bessel factor): EVR of PC1 (definition at the top of the page): PC1 explains of the variance.


L4 — Synthesis

Exercise 9

Start from raw (uncentered) data Carry out the full pipeline: center → covariance () → eigenvalues → PC1 → 1D projection. Comment on the second eigenvalue.

Recall Solution

Center: column means are . Covariance (): , so Eigenvalues: diagonal ⇒ . PC1: (the axis of the nonzero variance). Project: . Comment on : feature 2 is constant ( variance) — a degenerate direction carrying no information. PCA correctly assigns it eigenvalue , and 1D captures of variance (). This is exactly the "drop useless dimensions" use-case.

Exercise 10

You have two candidate 2D projections of the same centered data. Projection A uses the true top-2 PCs; Projection B uses two orthonormal but arbitrary directions. Which preserves more total variance, and can they ever tie?

Recall Solution

Intuition first (look at the figure below). Picture the total variance as a fixed "budget" of spread. The eigenvectors are the special axes that pile as much of that budget as possible into the first axis, then the most of what remains into the second, and so on. Any other orthonormal frame slices the same cloud at a tilt, so it spreads the budget more evenly — fine if you keep every axis, wasteful the moment you drop some.

Figure — PCA via eigendecomposition and SVD

The precise statement, sketched on this page. The variance captured by a single unit direction is Write in the eigenvector basis as with . Because and the are orthonormal, which is a weighted average of the eigenvalues with weights summing to . An average can never exceed its largest ingredient, so , with equality only when all the weight sits on (i.e. ). Stacking the next-best perpendicular direction gives , and so on — so for any orthonormal directions the captured variance is at most , achieved by the top- eigenvectors. (This "sum of the top eigenvalues is the best you can do" fact is the Ky-Fan result; we just proved the case above, and the general case stacks it.)

Conclusion: Projection A (true PCs) preserves at least as much variance as B, always: (variance of any other orthonormal frame).

When do they tie? When B happens to span the same subspace as the top- eigenvectors — e.g. if (a degenerate eigenvalue), any orthonormal pair in that plane is equally good, so B can match A. Ties also occur trivially when (both keep everything).


L5 — Mastery

Exercise 11

Prove that the total variance is invariant under any orthogonal change of basis (i.e. ). Then explain in one line why this justifies "keeping all PCs loses nothing."

Recall Solution

Tool: the cyclic property of trace, , and orthogonality . One-line justification: PCA is the special orthogonal rotation that makes diagonal; since total variance (the trace) is unchanged by any orthogonal rotation, moving into the PC basis and keeping all components preserves of variance — it only reorganizes where the variance lives.

Exercise 12

Show algebraically that projecting centered data onto a unit eigenvector (eigenvalue ) produces a set of scores with sample variance exactly .

Recall Solution

Setup: the score vector is . Its scores already have mean (because the data is centered and projection is linear), so its sample variance is Recognize the covariance: the middle chunk is , so Use the eigen-relation and unit length : The eigenvalue is the variance the component explains — the central claim of PCA, proven. (This is the identity Exercise 10 leaned on for its case.)

Exercise 13

A student computes covariance without dividing by anything, gets "eigenvalues" from samples. What are the true eigenvalues , and would the principal directions change?

Recall Solution

The scalar factor: the student used the raw scatter matrix instead of . Scaling a matrix by a positive constant scales every eigenvalue by that same constant, so the student's eigenvalues are times too large.

Correct the eigenvalues (divide by ):

Do the directions change? No. If , then — the same eigenvector , just a rescaled eigenvalue. Positive scaling never rotates eigenvectors.

And the EVR is untouched too: either way — the scalar cancels top and bottom.

Bottom line: the student's mistake corrupts the absolute variance values but not the principal directions nor the explained-variance ratios.

Recall Quick self-check

Eigenvalues sum to the trace ::: SVD to eigenvalue conversion ::: Distinct-eigenvalue PCs are ::: orthogonal (dot product ) A zero eigenvalue means ::: that direction has no variance (degenerate / redundant feature) EVR of the first k components :::


Where to go next

  • Struggling with the linear algebra itself? Revisit Linear-Algebra-Eigenvalues-and-Eigenvectors and Linear-Algebra-SVD.
  • Forgot why centering/scaling matters? See 2.4.03-Feature-Scalingand-Normalization.
  • Want a nonlinear cousin of these ideas? Try 2.6.04-Kernel-PCA and 3.2.05-Autoencoders.
  • For the big-picture role of PCA in unsupervised learning, return to 2.5.01-Introduction-to-Unsupervised-Learning and compare with 2.5.07-K-Means-Clustering.