TL;DR
Principal component analysis (PCA) rotates a cloud of data points so that its axes line up with the directions of greatest spread. The first axis captures the most variance, the second the most of what remains, and so on. Keeping only the first few axes gives a compact description of the data with the least possible squared error.
Where you'll see it: shrinking many correlated measurements to a few summary numbers, plotting high-dimensional data in two dimensions, and removing noise before a model is trained.
Intuition
A cloud with a long axis
Picture a scatter of points shaped like a cigar on a tilt. Each point has two coordinates, but most of the information lies along the cigar's length. The width is small, and it mostly records noise or small variations. If you could describe every point by its position along the cigar and ignore the width, you would lose little and gain a great deal of simplicity.
PCA finds that long axis automatically. It does not know what the data means. It only asks: in which direction does the data spread out the most? That direction becomes the first principal component. The second component is the direction of most remaining spread, constrained to be at a right angle to the first, and so on.
Why right angles
The components are forced to be perpendicular. This is what makes them useful: each one carries information the others do not, so their contributions add up without double counting. If you kept two components that pointed the same way, you would describe the same variation twice.
What PCA does not do
PCA finds directions of large variance, not directions of importance for a particular task. A feature with large variance that is pure noise will be kept, and a small-variance feature that predicts the label will be dropped. It is also a linear method: it can only rotate and squash the cloud, so a curved structure is not captured. Those limits matter, and they come up again in the tradeoffs section.
The math
Start with data points, each a vector in . Stack them as rows of a matrix of size .
First centre the data. Subtract the mean of each column so that every feature has mean zero. Call the centred matrix again. Centring means the directions we find are about spread around the average, not about the origin.
Next compute the Covariance Matrix of the centred data:
Here is the transpose of , so is a matrix. Entry is the covariance between feature and feature , and the diagonal holds each feature's variance.
Let be the eigendecomposition of (see Eigenvalues and Eigenvectors), with eigenvalues sorted from largest to smallest, , and the matching unit eigenvectors as the columns of . The -th principal component is the unit vector , the -th column of . The score of data point on component is , the Dot Product of the point with the component.
Here is the -th row of , a point in the original coordinates. The scores are its coordinates in the new, rotated system.
Why eigenvectors
The variance of the data along a unit direction is . Maximising this over unit vectors is the same as finding the eigenvector with the largest eigenvalue, because is symmetric and so has orthogonal eigenvectors. The maximum value is , so the first component carries variance . The next component is the best direction perpendicular to the first, and so on. This is a standard fact about symmetric matrices, and it is what makes the procedure well defined.
How much is kept
The total variance is the sum of the eigenvalues, . The fraction of variance kept by the first components is
Reducing the data to dimensions means keeping the scores , which is with holding the first columns of .
The reconstruction view
The same components give the best low-dimensional approximation. Projecting each point onto the first components and mapping back gives an approximation . Among all rank- linear approximations, this one has the smallest total squared error. The error left over is exactly the variance in the discarded components. So "keep the most variance" and "lose the least squared error" are the same goal.
Connection to the SVD
In practice PCA is computed with the Singular Value Decomposition (SVD) of rather than by forming . If , then the columns of are the principal components, and the eigenvalues of are . Avoiding is more accurate when is large or the data is nearly degenerate.
Worked examples
Take four points: , , and . They are already centred, since they average to zero.
- Compute the sums. , , and .
- Form the covariance with : , so .
- The eigenvalues are and . The eigenvectors are and . The first principal component is the horizontal axis.
- Fraction of variance kept by the first component: .
- Scores on the first component: . Reduce to one dimension, and the points are along that axis.
- Reconstruction error with one component: the residuals in the vertical direction are , so the squared error is . The total sum of squares is , and . This is the fraction that was discarded, which matches step 4.
Interpretation: the cross had its wide arm along the horizontal axis, so PCA chose that axis. Dropping the vertical axis kept 80 percent of the variance and lost 20 percent.
- first principal component
- data
Two features have covariance matrix . Each has variance , and they are positively correlated.
- Find eigenvalues by solving . This gives , so or .
- For , solve . The vector works, since . Normalise to .
- For , the vector works, since . Normalise to .
- Fraction of variance on the first component: .
Interpretation: the first component is the diagonal direction where both features rise together. Its score is roughly the average of the two features, and it keeps 75 percent of the total variance. The second component, the difference between the two features, carries the rest. Two correlated features are mostly one thing, and PCA finds that thing.
Tradeoffs and failure modes
PCA is fast, closed-form and easy to interpret, which makes it a default first step. It has several limits that show up in practice.
| Situation | What goes wrong | Better choice |
|---|---|---|
| Features on different scales (kilograms and metres) | The largest-unit feature dominates the components | Standardise each feature to unit variance first, which makes PCA work on the correlation matrix |
| Curved structure, such as a spiral | A linear projection flattens it and loses the shape | Kernel PCA, or a nonlinear autoencoder (see Autoencoder) |
| Task depends on low-variance directions | Those directions are discarded, and the label is lost | Keep the features, or choose components by their effect on the task |
| Outliers | A few extreme points can tilt the first component | Robust covariance estimates, or remove outliers before fitting |
| Very many features, few samples | The covariance matrix is poorly estimated | Compute with the SVD of the data, or regularise the covariance |
A common mistake is to choose the number of components without looking at the explained variance. Plot the cumulative fraction and keep enough components to meet a target, such as 90 percent, rather than picking a number by eye.
History
Karl Pearson introduced the idea in 1901, as a way to find the lines and planes that best fit a set of points. Harold Hotelling developed it into the component form used today in 1933, when he analysed correlated test scores and named the components. Michel Loève's work in the 1940s on expanding random processes in orthogonal functions is the same mathematics in another setting, which is why PCA is also called the Karhunen-Loève transform in signal processing. The method caught on once computers made the eigendecomposition cheap, and it is now a standard step in statistics and machine learning.
Check yourself
QuestionWhy are the principal components required to be perpendicular to each other?Show answer
So that each component carries variation the others do not. If two components pointed the same way, they would describe the same spread twice, and adding their scores would overcount it. Perpendicularity guarantees the components are uncorrelated and their variances add up to the total.
QuestionA two-feature dataset has covariance matrix diag(4, 1). What fraction of the total variance does the first principal component keep?Show answer
The eigenvalues are 4 and 1, so the total is 5. The first component keeps 4 divided by 5, which is 0.8, or 80 percent.
QuestionWhy does PCA usually standardise features with very different units before running?Show answer
PCA maximises variance, and variance depends on the units. A feature measured in millimetres has a numerically larger variance than the same quantity in metres, so it would dominate the first component for no real reason. Standardising makes each feature contribute on an equal footing.
QuestionMany points lie evenly spread around a circle. Why does one principal component describe their arrangement poorly?Show answer
PCA can only find straight directions. A circle has no preferred straight direction, so the variance is roughly the same along any axis and the first component captures little structure. A nonlinear method is needed for curved data.
QuestionFor centred data with a covariance of 3 along the first axis and 1 along the second, how much squared error does keeping only the first component leave, relative to the total variance?Show answer
The discarded variance is the second eigenvalue, 1, out of a total of 4. So the relative error is 1 divided by 4, which is 0.25. This is the same as the fraction of variance thrown away, because the reconstruction error equals the variance in the dropped components.
Sources
- Wikipedia: Principal component analysis
- Bishop, Pattern Recognition and Machine Learning, §12.1: https://www.microsoft.com/en-us/research/publication/pattern-recognition-machine-learning/