Image compression (eigenfaces, MNIST projection)
A 28×28 MNIST image lives in 784 dimensions but is well-approximated by ≈50 PCs. Same idea behind classical JPEG-like compression with the Karhunen–Loève transform.
Name things for the reader, not the writer.
Name things for the reader, not the writer.
Principal Component Analysis
Running PCA on real data requires a handful of practical steps that textbook treatments sometimes skip. First, center the data by subtracting the mean: tilde{mathbf{x}}_n = mathbf{x}_n - bar{mathbf{x}}. Without centering, the first 'principal component' will simply point toward the mean, not toward
Always center (and usually standardize) data before PCA.
Running PCA on real data requires a handful of practical steps that textbook treatments sometimes skip. First, center the data by subtracting the mean: . Without centering, the first 'principal component' will simply point toward the mean, not toward the direction of real variation. Second, usually standardize (divide each feature by its standard deviation) when features live on different scales — otherwise PCA is dominated by whichever feature happens to have the largest range.
How many components to keep? The scree plot shows eigenvalues in decreasing order. Look for an 'elbow' where eigenvalues flatten — components beyond the elbow are mostly noise. A more principled alternative is the cumulative explained variance: choose so that (or whatever threshold the downstream task requires). For purely visualization purposes, or is fixed by necessity.
Computationally, avoid forming explicitly when is large — it is and may not fit in memory. Instead, use SVD of $X$ directly: , where columns are principal components and are the eigenvalues of . For huge datasets, randomized SVD (Halko et al.) computes the top- components in time — orders of magnitude faster than full SVD.
PCA has important limitations. It is linear — it cannot capture curved manifolds (use kernel PCA, Isomap, UMAP, or autoencoders for that). It assumes variance = signal, which fails when noise has large variance (consider ICA instead). It is not scale-invariant — standardize carefully. And it is a lossy, unsupervised method; discarded components may contain exactly the features your downstream task needs. Always validate PCA choices against the downstream metric, not just explained variance.
Despite these caveats, PCA is the first tool to try for any high-dimensional dataset. Canonical applications: face recognition (eigenfaces), finance (principal portfolios / risk factors), genomics (population structure), NLP (latent semantic analysis is PCA on word-document matrices), image compression (JPEG's DCT is cousin to PCA). The machine-learning lesson is deeper than the algorithm: covariance matrices encode structure, and eigen-decomposition reveals it.
Worked example — rank-1 reconstruction error: A image has SVD , so the rank-1 approximation is exact — zero reconstruction error. Perturb to : singular values are , . Keeping only gives Frobenius reconstruction error , while the total 'energy' is — the top component explains of variance.
Watch reconstruction quality grow as you add components.
Python (in browser)
Expected: MSE drops, variance kept rises with k
Python runs entirely in your browser via Pyodide (~6 MB on first Run, cached after).
Worked example — choosing $k$ for 95% variance: Eigenvalues , total . Cumulative ratios: ; ; ; . So is the smallest number of components to retain 95% of the variance. The scree plot would show a clear elbow after the second component — a judgment call between aggressive compression (, 80%) and conservative preservation (, 95%).
A 28×28 MNIST image lives in 784 dimensions but is well-approximated by ≈50 PCs. Same idea behind classical JPEG-like compression with the Karhunen–Loève transform.
Fit PCA on normal data. New samples with high reconstruction error or large 'off-PC' residuals are flagged. Used in chemical-plant monitoring, network-intrusion detection.
PCA on intermediate activations of a deep network reveals which neurons are redundant — used for pruning, distillation, and interpretability.
Apply the kernel trick to PCA: do PCA in an implicit high-dimensional feature space defined by . Captures nonlinear manifolds.
from sklearn.decomposition import KernelPCA
kp = KernelPCA(n_components=2, kernel='rbf', gamma=0.05).fit_transform(X)Put your understanding to the test. Score + streak + speed all count.
3 quick questions. Get 2 right to mark this lesson complete.