Principal Component Analysis
Projecting data onto the directions of greatest variance for compression, denoising, visualization and faster models.
Intermediate · about 5 hours · difficulty 3/5
Learn first: Linear Algebra for ML, Encoding Categories and Scaling Numbers
PCA finds orthogonal directions (principal components) that capture the most variance, then lets you keep only the first few. It is linear algebra in action: eigenvectors of the covariance matrix, computed in practice with SVD.
Uses
- Visualization: plot the first two components to see structure.
- Compression and speed: fewer features for downstream models.
- Denoising: discard low-variance components that are mostly noise.
- Decorrelation of features before linear models.
How to apply
- Standardize features first unless they share units; otherwise the largest-scale feature dominates.
- Choose the number of components from the explained variance ratio (for example keep 95%).
- Fit PCA on training data only, inside a pipeline.
Limitations
Components are linear combinations and can be hard to interpret. PCA captures variance, which is not necessarily what predicts your target. Nonlinear structure needs other tools (kernel PCA, t-SNE, UMAP, autoencoders).
Good enough to move on when you can implement PCA from SVD in NumPy, match scikit-learn's result, and choose components from a cumulative variance plot.
Key concepts
- Variance and covariance
- PCA via SVD
- Explained variance ratio
- PCA in pipelines
- Kernel and incremental PCA
Mini project
Apply PCA to the Olivetti faces or MNIST digits, visualize the top components as images, and plot classifier accuracy against the number of components kept.
Check yourself
Why can PCA throw away a low-variance feature that is actually the most predictive of the target?