The directions it finds
PCA finds the orthogonal directions along which the data varies most, and describes each point by its coordinates along the first few of them.
- : the covariance matrix, .
- : the mean of the data, which must be subtracted first.
The eigenvectors of that matrix are the principal components. The eigenvalue attached to each one is the variance of the data along it.
Sort by eigenvalue descending, and the first component is the single direction of greatest spread.
What "explained variance" means
The eigenvalues sum to the total variance in the data, so each one's share is a real fraction rather than a score.
Keeping components until that ratio reaches 0.95 is the usual rule, and it is a decision about how much you are willing to throw away.
Projecting
- : the top eigenvectors as columns.
Each new coordinate is the dot product of the centred point with one component, so the new features are linear combinations of the old ones.
Projecting back gives the reconstruction, and the error it leaves is exactly the variance in the components that were dropped.
One run, in order
- Scale the features. PCA maximises variance, and variance is measured in the features' own units.
- Subtract the mean of each feature.
- Compute the covariance matrix, or take the SVD of the centred data directly.
- Sort the eigenvectors by their eigenvalues, descending.
- Keep the top and project.
In practice step three uses the SVD, because forming explicitly squares the condition number and loses precision for nothing.
What it cannot do
Every component is a linear combination of all the original features, so nothing is selected and nothing is interpretable. Component one is 0.3 of everything.
It is unsupervised: it maximises variance without knowing the labels, and the direction of greatest variance can easily be the direction that separates the classes least.
And it is linear. Data on a curved manifold is flattened rather than unrolled, which is what the nonlinear methods exist for.