Skip to content

1.2 Eigenvectors, SVD, PCA

ready≈ 10.5 hoursNeeds: 1.1Unlocks: 5.11, 6.1, 7.3
  1. 1 Learn
  2. 2 Bridge & derive
  3. 3 Explore (SVD image compressor, PCA point cloud)
  4. 4 Do
  5. 5 Check

An eigenvector of A\mathbf{A} is a direction the map does not turn: Av=λv\mathbf{A}\mathbf{v} = \lambda\mathbf{v}, where the eigenvalue λ\lambdaeigenvalueA scalar with Av = λv for some non-zero v (the eigenvector). In Part 2, λ is also the regularization strength; context decides.Introduced in 1.2 Eigenvectors, SVD, PCA. says how much it stretches (3Blue1Brown, ch. 14; MML §4.2). Not every matrix has real ones: a rotation turns everything. Symmetric matrices always do. Their eigenvalues are real and their eigenvectors can be chosen orthonormal (the spectral theorem, MML §4.2). Covariance matrices are symmetric, which is why PCA works.

The singular value decomposition A=UΣV⊤\mathbf{A} = \mathbf{U}\boldsymbol{\Sigma}\mathbf{V}^\top exists for every matrix, square or not: rotate, scale along the axes, rotate (MML §4.5). Written as a sum, A=∑iσiuivi⊤\mathbf{A} = \sum_i \sigma_i \mathbf{u}_i \mathbf{v}_i^\top is a stack of rank-one layers in order of importance. Keeping the top kk gives the best rank-kk approximation, with error ∑i>kσi2\sqrt{\sum_{i>k}\sigma_i^2} in the Frobenius norm, the square root of the sum of squared entries (Eckart–Young, MML §4.6). MML states the theorem in the spectral norm, the largest stretch of a matrix, where the error is just σk+1\sigma_{k+1}. That is the whole idea behind the image compressor below and the LoRA rank in module 5.11.

PCA finds the directions along which centred data varies most. The variance of the data projected onto a unit vector b\mathbf{b} is b⊤Sb\mathbf{b}^\top\mathbf{S}\mathbf{b}, where S\mathbf{S} is the covariance (MML’s notation; module 1.4 and the notation card call it Σ\boldsymbol{\Sigma}covariance matrixΣ_{ij} = Cov(x_i, x_j). Symmetric and positive semi-definite.Introduced in 1.4 Probability and statistics. Not to be confused with the summation sign ∑, or with the diagonal matrix of singular values in the SVD A = UΣVᵀ, which shares the symbol. MML §10 writes the data covariance as S., which is unrelated to the SVD’s Σ\boldsymbol\Sigma above). Maximising it gives an eigenvector, as derived below (MML §10.2). The variance you discard by keeping kk components equals the sum of the remaining eigenvalues.

Power iteration finds the top eigenvector by repeatedly applying S\mathbf{S} and renormalising. Each step shrinks the other components by the ratio ∣λ2/λ1∣|\lambda_2/\lambda_1|, so it is slow when the top two eigenvalues are close (MML §10.4, p. 334).

Notation trap: MML divides the covariance by NN. np.cov divides by N−1N-1 unless you pass bias=True. The tests use NN.

PCA: the maximum-variance direction is the top eigenvectorstep 1 of 5
  1. V(b)=1N∑n(b⊤xn)2=b⊤Sb,S=1N∑nxnxn⊤V(\mathbf{b}) = \frac{1}{N}\sum_n (\mathbf{b}^\top\mathbf{x}_n)^2 = \mathbf{b}^\top\mathbf{S}\mathbf{b}, \qquad \mathbf{S} = \tfrac1N \textstyle\sum_n \mathbf{x}_n\mathbf{x}_n^\top

SVD image compressor

Low-rank approximation and reconstruction error

Static view on small screens. Open this page on a laptop to interact.

original (rank up to 96)
rank-8 approximation
layer 8: u8σ8v8⊤

Loading…

Every image is a sum of rank-one layers, each an outer product scaled by a singular value. Keeping the k largest gives the best rank-k approximation (Eckart–Young). Stripes need one layer; a diagonal line needs almost all of them; noise has no structure to exploit.

A 96×96 image rebuilt from its top k rank-one layers, next to the k-th layer itself and the singular value spectrum. Readouts give the energy kept, the relative error and the storage cost. Keyboard: Tab to the slider, then arrow keys change k.

PCA point cloud

Principal axes are covariance eigenvectors

Static view on small screens. Open this page on a laptop to interact.

PC1PC2
Σ = [[3.21, 1.74], [1.74, 1.35]] λ₁ = 4.25, λ₂ = 0.31 PC1 explains 93.2% PC1 angle 30.9° (you set 30°) mean squared residual = 0.308 = λ₂

The axes are the eigenvectors of the covariance Σ, drawn to ±2√λ. Projecting onto PC1 throws away exactly λ₂ of variance per point: the residuals you see. Click empty space to add an outlier and watch the axes swing.

A 2-D point cloud with its principal axes drawn from the covariance eigenvectors. Orange marks show each point’s projection onto PC1, and the readouts compare the recovered angle with the one you set. Keyboard: sliders change the cloud; click (or tap) empty space to add an outlier.

Implement power iteration, the covariance, and PCA (fit, transform and inverse). Tests compare against np.linalg.eigh up to the sign of each eigenvector, and check that the reconstruction error equals the discarded variance.

PCA from scratch stubs/m1_2_pca.py

Loading starter file…

Prefer your own editor? Edit exercises/stubs/m1_2_pca.py and run mlc check 1.2. The tests are identical; import the results on the progress page.

Hints · pca_fit 0/3 revealed

Then try it on real data. With the tests passing, run PCA on MNIST digits locally. It downloads MNIST (about 11 MB) once.

Terminal window
cd exercises && python projects/pca_mnist.py

It prints how many components keep 50%, 90% and 99% of the variance of 784-pixel digits. Expect far fewer than 784. If matplotlib is installed, it also saves reconstructions.

1. Which matrices are guaranteed real eigenvalues and orthonormal eigenvectors?
2. Singular values of A are 5, 3, 1, 0.5. What is the Frobenius error of the best rank-2 approximation?
3. Covariance eigenvalues are 4, 1, 0. What fraction of variance does PC1 keep?
4. Power iteration converges slowly when…
  • not yet: All tests pass (0/?)
  • not yet: Checkpoint quiz ≥ 80%
Finish the items above first.
Learner log: time spent and points of confusion

Every confusion you note becomes a fix before the module is marked stable. Export it from the progress page and attach it to an issue.