Principal Component Analysis
PCA finds the directions in which data spreads out most, and they turn out to be the eigenvectors of its covariance matrix, because the spread along a direction is a quadratic form. Keeping only the top few directions lets you describe the data with fewer numbers while keeping most of what makes the points different.
What the symbols mean
- Covariance matrix
Symmetric; entry (i, j) measures how variables i and j vary together.
- Centered data
Each column is one observation with the mean subtracted.
- Variance along u_i
How much of the data's spread lies in that direction.
Explanation
Principal component analysis, or PCA, is simply a way to find the directions along which a cloud of data points is most spread out, so you can describe the data with fewer numbers. "Principal" here means main or most important, and the "components" are those directions, ranked from most spread to least. What it's good for: shrinking data with dozens or thousands of variables down to the few directions that actually matter, so you can compress it, speed up machine learning or plot it in 2D. All we're really saying here is that (the covariance matrix) records how the variables spread: its diagonal entries are each variable's variance, and its off-diagonal entries say how two variables move together. Then says the eigenvectors are the directions and the eigenvalues are how much spread lies along each one. Every piece is something from earlier in this unit; bear with the setup.
Think about why eigenvectors show up. The variance of the data along a unit direction works out to , which is a quadratic form. From the quadratic forms lesson, its largest value over unit vectors is the largest eigenvalue of , reached along the top eigenvector. And since is symmetric, the spectral theorem guarantees the principal components are perpendicular to each other.
A common mistake. It's tempting to skip subtracting the mean, because the data looks fine as it is. It isn't safe: without centering, measures spread around the origin instead of around the data's center, and the first "component" mostly just points from the origin toward the middle of the cloud.
Steps:
- Subtract the mean from each variable.
- Compute the covariance matrix .
- Find its eigenvalues and eigenvectors. The eigenvector with the largest eigenvalue is the first principal component: the direction of greatest spread.
Variance explained: the total variance is the trace of , the sum of the eigenvalues. The first components explain of it.
PCA is the SVD of the centered data matrix in disguise: the eigenvectors of are the left singular vectors of , so software usually computes PCA with the SVD directly.
There's an animation further down that shows the spread of the data's shadows changing as a line turns through the cloud.
See it
Worked examples
A dataset has covariance matrix . Find the principal components and the variance they explain.
- 1
The trace is 7 and the determinant is 6, so the characteristic equation is : .
Practice
Type your answer and check it. Solve them all to complete the lesson.
Find the eigenvalues of .
What fraction of the variance does the first principal component explain?
Give the first principal component as a unit vector with positive entries.
What is the total variance?
Eigenvalues are 9, 0.5 and 0.5. How many components keep at least 90% of the variance?
Four students' scores on two quizzes, after subtracting the class averages, are , , and . What fraction of the total variance does the first principal component capture?
The hardest one here. Get it, and you've got this lesson.
PCA compresses each centered data point to one number, its score , and reconstructs it as . For and the point , find the reconstruction error .
Where it's used
PCA compresses genetic data, reduces image features before machine learning, finds the "eigenfaces" in face recognition, and visualizes high-dimensional data in 2D.