ML Systems Lab Open interactive version →
Intermediate 26 min read PCAdimensionality reductioncovariance

PCA from First Principles

Covariance matrix, explained variance, when PCA fails

You are building a face recognition system. Each image is 100×100 pixels — 10,000 numbers per image. Training a model on raw 10,000-dimensional inputs is slow and prone to overfitting. But most of those dimensions carry redundant information: neighboring pixels are highly correlated, and faces share common structure — eyes roughly here, nose here, mouth here.

The naive fix is to drop some features. But which ones? Dropping pixel 4,512 and keeping pixel 4,513 is arbitrary — both carry similar information. What you want is to find the *directions* where faces actually vary, and represent each face as its coordinates along those directions.

PCA does exactly this. Centre the data (subtract the mean face). Compute the covariance matrix $Sigma = X^T X / (n-1)$ — a 10,000 × 10,000 matrix encoding how every pixel correlates with every other pixel. Find the eigenvectors of this matrix. The eigenvector with the largest eigenvalue is the direction along which face images vary most. Project every image onto the top $k$ eigenvectors. You have compressed 10,000 dimensions down to $k$ — say 50 — while retaining whatever fraction of variance those 50 directions explain. A scree plot of eigenvalues sorted in descending order shows the "elbow" where additional components stop explaining much variance.

Two things make PCA fail. First, scale: a feature measured in cents has 10,000× the variance of the same feature measured in dollars — converting dollars to cents multiplies every number by 100, and variance scales with the square of that factor (100² = 10,000×). PCA will identify "cents direction" as the first principal component — not because it contains more signal, but because its numbers are larger. Always standardise (z-score) before running PCA unless features share a natural common scale.

NOT this. Most people think "PCA removes correlated features." PCA does not select a subset of original features. It creates entirely new features that are linear combinations of *all* original features. The new features — principal components — are uncorrelated with each other by construction. But each PC mixes every original feature together. You cannot look at a principal component and say "this is pixel 4,512." The confusion matters because PCA cannot be used for feature selection if you need interpretability in the original feature space.

Key points

Takeaway

PCA keeps high-variance directions and discards low-variance ones. Always verify that the discarded variance does not contain the label signal — the information your classifier needs most may live exactly in the directions PCA throws away.

Recap

Check your understanding

Q1. You have a dataset with covariance matrix Σ. Describe the PCA algorithm as an eigendecomposition problem, and explain what the principal components represent.

Q2. PCA on a dataset with 1000 features gives first two PCs explaining 95% of variance. A colleague uses these 2 PCs as features for a random forest. Which TWO of the following correctly describe what might go wrong?

Q3. Why must you subtract the mean before applying PCA? What goes wrong if you do not?

Try it interactively

ML Systems Lab is a free interview-prep platform for ML engineers — work through the full interactive module, quizzes, and drills.

Open ML Systems Lab →