1.2 Eigenvectors, SVD, PCA
1. Learn
Section titled “1. Learn”- 3Blue1Brown, Eigenvectors and eigenvalueswhole lesson (chapter 14)
- Mathematics for Machine Learning (Deisenroth, Faisal, Ong)§4.2 Eigenvalues and Eigenvectors (pp. 105–114), §4.5 Singular Value Decomposition (pp. 119–129), §4.6 Matrix Approximation (pp. 129–134)
- Mathematics for Machine Learning (Deisenroth, Faisal, Ong)§10.1–10.2 Problem Setting and Maximum Variance Perspective (pp. 318–325), §10.6 Key Steps of PCA in Practice (pp. 336–339)
- Mathematics for Machine Learning (Deisenroth, Faisal, Ong)§7.2 Constrained Optimization and Lagrange Multipliers, for the PCA derivation
2. Bridge
Section titled “2. Bridge”An eigenvector of is a direction the map does not turn: , where the eigenvalue eigenvalueA 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 exists for every matrix, square or not: rotate, scale along the axes, rotate (MML §4.5). Written as a sum, is a stack of rank-one layers in order of importance. Keeping the top gives the best rank- approximation, with error 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 . 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 is , where is the covariance (MML’s notation; module 1.4 and the notation card call it 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 above). Maximising it gives an eigenvector, as derived below (MML §10.2). The variance you discard by keeping components equals the sum of the remaining eigenvalues.
Power iteration finds the top eigenvector by repeatedly applying and renormalising. Each step shrinks the other components by the ratio , so it is slow when the top two eigenvalues are close (MML §10.4, p. 334).
Notation trap: MML divides the covariance by . np.cov divides by unless you pass bias=True. The tests use .
3. Explore
Section titled “3. Explore”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.
stubs/m1_2_pca.pyLoading 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.
Then try it on real data. With the tests passing, run PCA on MNIST digits locally. It downloads MNIST (about 11 MB) once.
cd exercises && python projects/pca_mnist.pyIt 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.
5. Check
Section titled “5. Check”- not yet: All tests pass (0/?)
- not yet: Checkpoint quiz ≥ 80%
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.