Sub topic 12
Reading loadings and scores
This chapter assumes you can fit PCA with scikit-learn and know what explained variance means. Here you will learn to read the two outputs PCA gives you: scores describe the samples, and loadings describe the features.
Scores and loadings
A score is the coordinate of one sample along a principal component. The score matrix Z has one row per sample and one column per component. Use scores to plot samples, find clusters or outliers, or feed a downstream model.
A loading says how much one original feature contributes to one component. The rows of components_ in scikit-learn are unit-length weight vectors, so each component is a weighted sum of the centered features. Use loadings to explain what a component means.
| Output | Shape | Describes | Question it answers |
|---|---|---|---|
| Scores `Z` | samples × components | Samples | Where does this sample sit? |
| `components_` | components × features | Features | Which features build this axis? |
Computing loadings
The raw weights in components_ all have length 1. Many texts instead define loadings as the weights scaled by the square root of each eigenvalue. When the data was standardized first, these scaled loadings are the correlations between each feature and each component. That makes them easy to compare across features.
import numpy as np
from sklearn.datasets import load_iris
from sklearn.decomposition import PCA
from sklearn.pipeline import make_pipeline
from sklearn.preprocessing import StandardScaler
data = load_iris()
X = data.data
names = data.feature_names
pipe = make_pipeline(StandardScaler(), PCA(n_components=2))
Z = pipe.fit_transform(X)
pca = pipe.named_steps["pca"]
loadings = pca.components_.T * np.sqrt(pca.explained_variance_)
print("feature PC1 PC2")
for name, row in zip(names, loadings):
print(f"{name:20s} {row[0]:6.2f} {row[1]:6.2f}")
print(Z.var(axis=0, ddof=1))
print(pca.explained_variance_)The last two lines print the same numbers. The variance of each score column equals the eigenvalue for that component.
On iris, PC1 should show large loadings of the same sign for sepal length, petal length and petal width. Sepal width should have a smaller loading of the opposite sign. Read PC1 as an overall flower size axis. PC2 should be dominated by sepal width.
Reading them correctly
- Look at the sign pattern within a component. Features with the same sign move together along that axis. Opposite signs mean a contrast.
- Look at magnitude. Loadings near ±1 mean the feature is almost the same as the component. Loadings near 0 mean the feature plays little part.
- Check how much of each feature is kept. Sum a feature's squared loadings over the kept components. The result is the fraction of that feature's variance the reduced data retains. A feature with a low value is poorly represented.
kept = (loadings ** 2).sum(axis=1)
print(dict(zip(names, kept.round(2))))Common errors
- Reading the sign as meaning. The sign of a whole component is arbitrary. Flipping every loading and every score in one component describes the same solution, and different library versions can disagree. Interpret only the relative signs inside a component.
- Mixing up the two scalings. Unit-length weights and eigenvalue-scaled loadings differ. State which one you report. Correlation-style interpretation needs the scaled version.
- Interpreting loadings from unscaled data. Without standardizing, a feature with a large numeric range dominates PC1. Its loading then reflects units, not structure.
- Over-reading small differences. Components with close eigenvalues are unstable. A small change in the data can rotate them within their shared plane, so their individual loadings mean little.
- Treating a component as a cause. A component is a statistical summary. A name such as "size" is your interpretation and can be wrong.
Exercise
A standardized dataset has PC1 loadings of 0.9 for height, 0.8 for weight and -0.1 for shoe-size rank. A colleague flips every sign in PC1 and says the result is a different analysis. Are they right, and what does PC1 describe?
Show answer
They are wrong. Flipping all signs in a component gives the same axis pointed the other way, with the same explained variance and the same relationships. PC1 mostly describes a combined height and weight dimension, because those two features have large loadings of the same sign. Shoe-size rank barely contributes.