The idea
Imagine photographing a long, thin pencil. From the side you see its full length; from the end you see a dot. PCA finds the side-on view: the angle that shows the most spread.
That best direction is the first principal component, PC1. The second, PC2, is at right angles to it and captures whatever spread is left. Dropping PC2 squashes every point onto a line, the simplest possible summary.
How it works
- Centre the data by subtracting the mean point.
- Compute the covariance matrix of and .
- Find its eigenvectors (the directions) and eigenvalues (the variance along each).
- Sort them: the largest eigenvalue gives PC1, the other gives PC2.
- Project each point onto PC1 to get its one-number summary.
Reading the picture
Two arrows start at the mean: PC1 along the longest spread and PC2 at right angles to it. Their lengths reflect how much variance each direction holds.
With Project onto PC1 on, each point is joined to its shadow on the PC1 line. Those short segments are the information PCA throws away; the stats show what share of the variance PC1 keeps.
Try this
- On Ellipse, drag points to rotate the cloud and watch PC1 turn with it. The PC1 angle in the stats follows along.
- Make the cloud rounder by dragging points outwards along PC2. PC1 explains less and less as the shape approaches a circle.
- Try Blobs. PC1 runs through the clusters, showing PCA ignores labels and only cares about spread.
- Drag one point far away from the rest. A single outlier can swing PC1 towards it.
The maths
Covariance
Components
Explained variance
Projection
Reconstruction
Strengths and weaknesses
Good at
- Fast, exact and has no hyperparameters.
- Compresses data while keeping the most variance.
- Useful for visualising and de-noising.
Struggles with
- Only finds straight-line directions.
- Sensitive to outliers and to feature scaling.
- Most spread is not always most useful.