PCA: the shape hidden in your data
The geometry behind principal component analysis: how the covariance matrix encodes the shape of your data, and why eigenvectors are the natural axes to read it from.
An NBA scout records three measurements for every player he is scouting: height, wingspan and reach. The thing is that all these measures are highly correlated because taller players tend to have longer wingspans and higher reaches. So in practice there are three numbers (dimensions) per player, but the data could be represented with a single dimension representing the “overall size” of the NBA player. PCA finds this dimension automatically from the data alone.
How the data was generated
=
= 220
=
= # cm
= # cm
# Correlation matrix: positively correlated but not perfectly
=
=
= @
= * +
Standardising the data
For computing the PCA components, we need the covariance matrix. But for the covariance matrix, each feature needs to be standardized. The data points are standardized as
where indexes the player and indexes the feature (height, wingspan, or standing reach), is the mean of feature across all players, and is its standard deviation.
This step makes each variable have a mean of 0 and variance of 1. This makes the three axes comparable because otherwise larger units would artificially dominate the covariance matrix. The shape of the correlation stays unchanged because standardisation only rescales the values and doesn’t distort them.
The covariance matrix: encoding shape
After standardising the three measurements, the sample covariance matrix is computed as:
This form holds because is already centered. Since each feature also has variance 1, the diagonal entries of are all 1 and the off-diagonal entries are Pearson correlation coefficients. For standardized data, the covariance matrix and the correlation matrix are the same thing. Geometrically the covariance matrix encodes the shape of the data cloud. Looking at Listing 1, you can see that the sample covariance matrix is basically the same that was used to generate the synthetic data. You can see the resulting covariance matrix from Figure 3.
Eigenvectors: the natural axes of the data
An eigenvector of satisfies
In practical terms, this means that applying the matrix to only scales the vector by a scalar and never rotates it. These eigenvectors are natural axes for the data cloud. These eigenvectors are directions along which the data varies independently with no covariance between them (orthogonal with respect to each other). The corresponding eigenvalue is the variance of the data projected onto that axis. Then sorting the eigenvalues from largest to smallest gives us the principal components in order: PC1 explains most of the variance, PC2 the next most etc. All principal components are orthogonal to each other. The full decomposition is:
where has the eigenvectors as columns and .
Figure 4 shows the three principal component arrows as eigenvectors, scaled by to reflect the data spread better. When rotating the 3d visual, you can see that the data cloud stretches along PC1 and PC2 and PC3 explain the remaining smaller variation.
Computing the principal components
, =
# eigh returns ascending order — reverse to descending
=
=
= # columns are PC vectors
= /
=
Eigenvalues: [2.402 0.385 0.227] Explained variance:[0.797 0.128 0.075] PC1: [-0.592 -0.585 -0.555] PC2: [-0.314 -0.466 0.827] PC3: [-0.742 0.664 0.092]
Explained variance and the scree plot
Each eigenvalue is the variance of the data projected onto PC. Thus the fraction of total variance explained by PC is . Figure 5 shows these fractions in descending order.
Dimensionality reduction
Keeping only the first principal components gives a rank- approximation of the data:
where contains only the first eigenvectors and is the number of features (3 in this case). In practice, the data is first projected down to dimensions, then reconstructed back into the original -dimensional space. But during the reconstruction, the data is constrained to lie on a -dimensional subspace, so information carried by the discarded eigenvalues is lost. The reconstruction error is exactly the sum of these discarded eigenvalues:
What the scout found
PC1 captures nearly all variation (~80%, see Figure 5) of the three measurements. PC1 can be thought of as an “overall size” axis. PC2 and PC3 capture the remaining ~20% of the variation. The beautiful part of PCA is that all of this analysis could be done automatically with only the data set and a little bit of clever linear algebra. The method found the dominant axis automatically.