The idea
A centered cloud of points in dimensions usually does not fill all dimensions equally — it is elongated some ways, thin others. PCA finds the cloud's natural axes: the first principal component is the direction of maximal variance, the second is the most-variance direction perpendicular to the first, and so on. Keeping the top axes and dropping the rest is the best linear compression of the data in a sense made precise below.
The objective, and why eigenvectors appear. Project centered data onto a unit direction ; the projected variance is where is the covariance matrix. Maximize it subject to with a Lagrange multiplier:
The stationary points are eigenvectors of , and the variance achieved at an eigenvector is its eigenvalue (). So the top eigenvector wins, the second component is the next eigenvector, and orthogonality is not an extra assumption — it is delivered free by the spectral theorem, since is symmetric.
Why "maximize variance" is not arbitrary. Total variance is fixed by the data. Each point splits, by Pythagoras, into its projection onto the kept subspace plus a perpendicular residual:
Maximizing kept variance and minimizing squared reconstruction error are the same optimization. PCA is exactly the -dimensional linear view that loses the least of the data — which is also why it coincides with the truncated SVD of the centered data matrix.
The recipe (and where each classic mistake hides):
- Center features with training-set means (without centering, PCA decomposes the second moment and the leading component may be dominated by the mean offset rather than variation around the mean).
- Standardize if features have incomparable units (skip → big-unit features hijack the components).
- Decompose: SVD of in practice, . Directions = columns of ; variances ; scores = .
- Pick from the explained-variance ratio , an elbow, or downstream cross-validation.
- Transform new data with the same means, scales, and — refitting on test data is leakage.
See it happen in three dimensions — the plane below is fit live from the cloud's own covariance matrix, then every point collapses onto it:
Loading the interactive 3D PCA-projection visualization…
Worked example
Four points, already centered (mean is the origin): .
Covariance (divisor ):
Eigenpairs. By symmetry the eigenvectors are the diagonals:
The explained-variance ratio of PC1 is
Scores on PC1 ():
The score variance checks the eigenvalue directly:
Reconstruction with . Rebuild each point as : and lie exactly on the PC1 line, so they reconstruct perfectly. and are perpendicular to PC1, so both collapse to the origin, each with squared error . Average the squared errors with the same divisor as the covariance: — exactly the discarded eigenvalue, confirming kept variance () plus reconstruction error () equals total variance ().
The scaling trap in one line. Rescale the second feature by 100 (say, meters to centimeters) and redo this: the top component snaps to nearly regardless of the correlation structure — units, not signal, chose the axes. That is why you standardize mixed-scale data first.
Cheat sheet
Formulas
- Covariance: (centered ).
- Components: ; variance along is ; components orthogonal (spectral theorem).
- Explained variance ratio: ; keep by cumulative target, elbow, or CV.
- Via SVD: ; directions , , scores .
- Whitening: divide scores by → identity covariance (helps some downstream models, amplifies noise dimensions).
- Equivalences: max variance min squared reconstruction error truncated SVD of .
Symptom-to-cause table
| Symptom | Likely cause | Fix |
|---|---|---|
| PC1 points at the data mean | forgot to center | center with train-set means |
| PC1 = the feature with biggest units | no standardization | standardize (correlation PCA) |
| Components flip sign between runs | inherent sign ambiguity | fix a sign convention; not a bug |
| Great explained variance, worse classifier | variance is not class signal | sweep by CV; try LDA; consider no PCA |
| Test metrics too good pre-deploy | PCA fitted on all data | fit on train split only |
| One weird component | outliers steering squared error | robust scaling / investigate outliers first |
Gotchas
- PCA is unsupervised: high variance is not the same thing as useful signal, and the top component can be pure nuisance (batch effects, exposure, document length).
- Loadings are directions, not causes — do not over-interpret component "meanings", especially after standardization.
- PCA on one-hot dummies is usually a smell; components destroy the sparsity and the geometry that made the encoding useful.
- With there are at most nonzero eigenvalues — the rest of the spectrum is exactly zero, not small.
- Correlation-PCA and covariance-PCA give genuinely different components; the choice is a modeling decision, not a numerical detail.