Dimensionality Reduction with PCA
Data usually spreads out much more in a few directions than in the rest. PCA finds those directions: they're the eigenvectors of the covariance matrix, and each eigenvalue is how much variance its direction holds, so you can keep the big ones and drop the rest.
What the symbols mean
- Covariance matrix
Entry (j, k) is how features j and k vary together; the diagonal holds each feature's variance.
- Centered data
The data table with each column's mean subtracted, so every feature averages 0.
- Number of examples
How many rows (data points) there are.
What the symbols mean
- Principal direction
A unit vector the data spreads along; the eigenvectors are perpendicular to each other.
- Variance along v
The eigenvalue: the variance of the data projected onto v.
What the symbols mean
- Largest eigenvalue
The variance along the first principal component.
- Number of features
The dimension of the data; the denominator is the total variance.
Explanation
PCA (principal component analysis) is simply a way to describe data with fewer numbers by finding the directions it's most spread out along. The name says it: the principal components are the main directions of the data, ranked by how much of the spread each one captures. In , the pieces are (the covariance matrix: how each pair of features varies together), (a direction) and (how much the data spreads out along that direction). If the first two or three directions hold almost all the spread, you can describe each data point by two or three numbers instead of hundreds. It's an eigenvalue problem you already know how to solve, applied to data.
Center first. Subtract each feature's mean so the cloud of points sits around the origin. Then the covariance matrix is : entry is , the average product of features and . Diagonal entries are variances, off-diagonal ones are covariances. (Dividing by instead of is the usual sample convention; it scales every eigenvalue the same way, so the directions don't change.)
Variance along a direction. Pick a unit vector . Each centered point projects onto it as the single number (a dot product, its shadow on the line). These numbers average , since the data is centered, so their variance is
So the whole question "which direction spreads the data most?" is: maximize subject to .
Why the answer is an eigenvector. That's a constrained maximum, the setting of Lagrange Multipliers. The condition is , which works out to , or . So the best direction is an eigenvector of (see Eigenvectors and Eigenvalues). And the variance it captures is . Pick the eigenvector with the biggest eigenvalue and you have the first principal component. The next components are the other eigenvectors, in order of eigenvalue. Since is symmetric, they're perpendicular to each other, so each new component captures spread the earlier ones missed.
How much to keep. The total variance is the sum of the diagonal of , which also equals the sum of its eigenvalues. Keeping the top components keeps of it; the explained variance ratio is that over the total. A common rule keeps enough components for 90% or 95%. What you throw away is exactly the rest: the squared distances from the points to their compressed versions add up to .
The SVD connection. In practice nobody forms . Write the centered data as (the SVD). Then , so . The columns of are the principal directions, and each eigenvalue is . Computing the SVD directly is more accurate than computing and then its eigenvectors. The linear-algebra side of all this is in Principal Component Analysis.
A common mistake. It's tempting to run PCA on raw data. Forget to center it and the first "component" just points at the mean. Forget to put features on a common scale and PCA chases units instead of structure: measure one feature in millimeters and another in meters, and the millimeter feature has a million times the variance, so it wins the first component whether it matters or not. Standardize first unless the units are already comparable.
There's an animation further down where you rotate a direction through a cloud of points and watch the variance of the projections.
See it
Worked examples
Two sensors give the readings , , , . Find the covariance matrix, the first principal component and its explained variance ratio.
- 1
Check that the data is centered. The values average and the values average . Good: no mean to subtract.
PCA on a set of product images gives eigenvalues . How many components keep at least 95% of the variance?
- 1
The total variance is the sum of all the eigenvalues: .
Practice
Type your answer and check it. Solve them all to complete the lesson.
For the data points , , , find the covariance of and (divide by ).
A covariance matrix is . What fraction of the variance does the first principal component explain? Give 4 decimal places.
PCA eigenvalues are . What is the smallest number of components that keeps at least 90% of the variance?
With covariance matrix , what is the variance of the data along the unit direction ?
A centered data matrix with 101 rows has singular values , and . What fraction of the variance do the first two principal components explain? Give 4 decimal places.
A factory logs temperature and pressure at a machine, already standardized, with covariance matrix . Engineers want to monitor one combined number instead of two. What fraction of the variance does the first principal component keep? Give 4 decimal places.
A dataset has a "length" feature in millimeters (values around 5000) and a "weight" feature in kilograms (values around 2). You run PCA on the raw numbers. What's most likely to happen?
For , at what angle (in degrees, from the positive -axis) does the first principal direction point? Give 2 decimal places.
The hardest one here. Get it, and you've got this lesson.
Compress the four points , , , to one number each with PCA, then map those numbers back to the plane. What is the total squared reconstruction error ?
Where it's used
PCA is the standard first step for squeezing high-dimensional data: hundreds of correlated sensor channels on an engine become a handful of components to monitor, face images become "eigenfaces," and a 768-number text embedding gets projected to 2D so people can see its clusters on a screen. Training on the top components instead of raw features also speeds up later models and filters out noise, since the small-variance directions are often mostly noise.