- PCA centers the data and finds orthogonal directions of maximum variance.
- The first principal component is the eigenvector with the largest eigenvalue of the covariance matrix.
- Projection compresses the data; reconstruction throws away the discarded directions.
This lesson follows Gaussian Mixtures & EM and leads into Neural Networks. The geometry lives in Linear Algebra and Least Squares.
1. Which direction?
A cloud of points can be described with many coordinate systems. If the cloud is stretched along a diagonal, the original x and y axes are a clumsy summary: both coordinates change together. PCA rotates the axes so the first one points along the longest direction.
The goal is compression. If the first direction captures most of the spread, we can store one coordinate per point and discard the second with little loss. The cost is that the new coordinate is a mixture of the original features, not a single measured variable.
Before reading the formulas, look at the animation. Which direction would you draw through the cloud if you had to summarize it with one line?
2. Center and covariance
PCA starts by subtracting the mean. The center becomes the origin:
The covariance matrix summarizes how the features vary together. The diagonal entries are variances; the off-diagonal entries are covariances:
A positive off-diagonal covariance means the features tend to rise together. A negative one means one tends to fall when the other rises. PCA uses the covariance geometry, so scaling matters: a feature measured in millimetres can dominate one measured in metres.
3. Eigenvectors
The principal directions are the eigenvectors of the covariance matrix:
The eigenvalue λ_j is the variance along that direction. Eigenvectors are orthogonal for a symmetric covariance matrix, so the new axes are perpendicular. The sign of an eigenvector is arbitrary: v and −v describe the same line.
In two dimensions, the first eigenvector maximizes the projected variance; the second is forced to be perpendicular and captures what remains. In higher dimensions, the same ordering continues.
4. Project and reconstruct
Projecting a centered point onto the first component gives a scalar score:
The score is the coordinate of the point in the new system. Reconstructing from one component puts the point back on the principal line:
Reconstruction is lossy when the discarded components carried variance. The difference between the original point and its reconstruction is the residual that PCA chose to drop.
5. Worked example
Three points on a line: (1,1), (2,2), (3,3). The mean is (2,2). The covariance matrix has 2/3 in every entry:
Worked example: one component is enough
| point | centered | PC1 score | reconstruction |
|---|---|---|---|
| (1, 1) | (-1, -1) | -1.414 | (1.000, 1.000) |
| (2, 2) | (0, 0) | 0.000 | (2.000, 2.000) |
| (3, 3) | (1, 1) | 1.414 | (3.000, 3.000) |
The eigenvalues are 4/3 and 0. The first eigenvector is (1/√2, 1/√2), so one component captures all the variance. The second is perpendicular and carries nothing.
This is a deliberately exact example. Real clouds have noise, so the second eigenvalue is positive and the reconstruction has a residual.
The animation’s cloud is noisier: PC1 captures most, but not all, of the variance. The explained-variance label shows the fraction retained by the first component.
6. How many components
The explained variance of component j is its eigenvalue divided by the total variance:
Choose a target such as 90% or 95% of the variance, or use a validation task to decide. A scree plot shows eigenvalues in descending order; the “elbow” is another heuristic, not a theorem.
Keeping enough components to explain variance is not the same as keeping enough to predict a target. A low-variance direction can be the most useful feature for classification. PCA is unsupervised, so it does not know your label.
Why is the first principal component not always the best feature for prediction?
7. Where it fails
PCA is linear. A curved manifold can be poorly summarized by a line or plane. Kernel PCA and autoencoders are nonlinear alternatives, with their own assumptions and costs.
Outliers can rotate the principal axes because variance is squared. Scaling changes the answer. The components are linear combinations of all features, so they are often harder to interpret than the original measurements.
A component’s sign has no meaning, and “explained variance” is not “explained causality.” PCA describes covariance; it does not tell you which feature to intervene on.
The next lesson returns to supervised learning with a flexible function class: neural networks.
Next lesson: Neural Networks
8. Going deeper
PCA is often computed with the singular value decomposition of the centered data matrix. The right singular vectors are the principal directions, and squared singular values are proportional to the eigenvalues. This is numerically more stable than forming the covariance matrix explicitly.
Probabilistic PCA models the data as a low-dimensional latent variable plus Gaussian noise. It provides a likelihood and a natural way to handle missing data. Kernel PCA applies the same idea in a feature space defined by a kernel.
Whitening rescales the projected components to unit variance. It can help downstream models that assume isotropic features, but it also amplifies directions with little data and can increase noise.
9. Practice
What happens to PCA if you multiply one feature by 1000?
Why are the principal axes perpendicular?
If PC1 explains 99% of the variance, can you safely drop PC2?
10. Resources
The scikit-learn decomposition chapter covers exact PCA, randomized PCA, whitening, and kernel PCA. Explained Visually gives an interactive 2D and 3D tour of the principal axes. Both links were opened and checked for this lesson.
Continue the track
The mathematics behind this topic: Linear algebra · Least squares.