Sub topic 12
Reading Loadings and Scores
What this chapter assumes
You should know how to fit PCA with scikit-learn, as in the previous chapter. You should also understand standardization and explained variance, and be comfortable with matrix shapes (samples as rows, features as columns). This chapter covers what to do after the fit: how to read the two outputs that describe it.
- Loadings describe the components in terms of your original features.
- Scores describe your samples in terms of the components.
Two views of the same fit
A fitted PCA gives you two kinds of numbers. One says what each component is made of. The other says where each sample sits along each component. Mixing them up is a common source of confused interpretations.
| Object | Shape | Where to get it | Question it answers |
|---|---|---|---|
| Axes (unit eigenvectors) | components × features | `pca.components_` | Which direction in feature space is this component? |
| Loadings | features × components | `pca.components_.T * np.sqrt(pca.explained_variance_)` | How strongly is each feature tied to each component? |
| Scores | samples × components | `pca.transform(X)` | Where does each sample fall along each component? |
| Eigenvalues | components | `pca.explained_variance_` | How much variance does each component carry? |
Axes versus loadings
The word "loading" is used in two ways, and libraries disagree.
- Some texts call the unit-length eigenvectors the loadings. In scikit-learn these are
components_. - Others scale each eigenvector by the square root of its eigenvalue. This is the convention used here.
The scaled version has a useful meaning. If you standardized the features before PCA, the scaled loading of feature j on component k is the correlation between that feature and the component's scores. A value of 0.9 means the feature and the component move together almost perfectly. A value near 0 means the component says little about that feature.
The unit-length axes are still useful. They are the weights in the formula that produces a score, so they tell you how to compute a component from raw features. The scaled loadings are easier to compare across components, because they are on a correlation scale from -1 to 1.
The next script fits PCA on standardized iris data and prints both versions. It also checks the correlation claim directly.
import numpy as np
from sklearn.datasets import load_iris
from sklearn.decomposition import PCA
from sklearn.preprocessing import StandardScaler
data = load_iris()
names = data.feature_names
X_std = StandardScaler().fit_transform(data.data)
pca = PCA(n_components=2).fit(X_std)
scores = pca.transform(X_std)
axes = pca.components_.T
loadings = axes * np.sqrt(pca.explained_variance_)
print(f"{'feature':<20} {'PC1 axis':>9} {'PC2 axis':>9} {'PC1 load':>9} {'PC2 load':>9}")
for name, a, l in zip(names, axes, loadings):
print(f"{name:<20} {a[0]:9.3f} {a[1]:9.3f} {l[0]:9.3f} {l[1]:9.3f}")
corr = np.array([[np.corrcoef(X_std[:, j], scores[:, k])[0, 1]
for k in range(2)] for j in range(4)])
print(np.allclose(corr, loadings, rtol=0.01))The final line should print True. The match is not exact. StandardScaler divides by n while PCA divides by n − 1, which makes the loadings about 0.3% too large for iris.
Reading a loading table
Read one component at a time, down its column. Look for the features with the largest absolute values, then check their signs.
For standardized iris, the PC1 axis is roughly 0.52, -0.27, 0.58 and 0.56 for sepal length, sepal width, petal length and petal width. The signs may be flipped on your machine. Three features share a sign and one has the opposite sign. PC1 therefore behaves like an overall flower-size contrast: large petals and long sepals on one end, wide sepals on the other. PC2 is dominated by sepal width (axis about 0.92) and is close to a single-feature direction.
A practical routine:
- Sort the features of each component by absolute loading.
- Pick the few that stand out, often those above about 0.4 in magnitude.
- Note which of them share a sign. Features with the same sign increase together along the component. Features with opposite signs trade off against each other.
- Write a short plain-language label, and treat it as a hypothesis, not a fact.
Remember that every component is a combination of all the features. A loading near zero says the feature barely correlates with that component. It does not say the feature is unimportant for other components.
Reading scores
Scores are the coordinates of each sample in the new basis. Row i of the score matrix is sample i described by its position along PC1, PC2 and so on. Three properties are worth checking, and each one catches mistakes.
import numpy as np
from sklearn.datasets import load_iris
from sklearn.decomposition import PCA
from sklearn.preprocessing import StandardScaler
data = load_iris()
X_std = StandardScaler().fit_transform(data.data)
pca = PCA(n_components=2).fit(X_std)
scores = pca.transform(X_std)
print(np.allclose(scores.mean(axis=0), 0, atol=1e-10))
print(np.allclose(scores.var(axis=0, ddof=1), pca.explained_variance_))
print(np.allclose(np.corrcoef(scores, rowvar=False), np.eye(2), atol=1e-8))
for k, label in enumerate(data.target_names):
print(label, scores[data.target == k].mean(axis=0).round(2))All three checks print True:
- Scores have mean zero, because PCA centers the data.
- The variance of each score column equals that component's eigenvalue.
- Score columns are uncorrelated. This is the defining property of principal components.
The last loop shows how to use scores. Averaging them by class shows that one iris species sits far from the other two on PC1, while the other two differ mostly in a smaller way. The groups were never given to PCA. The structure comes from the features alone.
To describe a single sample, look at its score on each component and combine that with the loading reading. A sample with a large positive PC1 score is, in terms of the PC1 label, high on the size contrast. If PC1 has flipped sign on your machine, "high" and "low" swap. This is why you should read scores and loadings from the same fit.
Plotting them together: the biplot
A biplot draws scores as points and loadings as arrows from the origin on the same axes. An arrow's direction shows which way that feature increases in the plot. A long arrow means the two plotted components capture that feature well. Arrows that point the same way indicate correlated features. The plot is only faithful if the two components explain most of the variance, so check the explained variance ratio before trusting it.
Pitfalls
- Sign is arbitrary. An eigenvector and its negative are equally valid. Different library versions, solvers or random seeds can flip a component. Never compare signs across separate fits, and fix a convention if you need stable output, for example by forcing the largest loading to be positive.
- Rotation between similar eigenvalues. If two eigenvalues are close, the individual axes are unstable and any rotation within that plane is nearly as good. Interpret such components as a pair, not separately.
- Scale changes the loadings. On unstandardized data, features with large units dominate the first components, so the loadings mostly reflect units. Standardize before interpreting, unless the raw units have a real meaning.
- Raw weights are not correlations. Comparing
components_entries between components is misleading because every axis has unit length. Use scaled loadings to compare strength. - Correlation is not causation. A component label such as "size" describes a pattern in the data. It does not prove an underlying factor exists. PCA is not factor analysis.
- Scores for new data. Transform new samples with the scaler and PCA fitted on the training data. Refitting on new data changes the axes and makes scores incomparable.
Exercise
A feature was standardized before PCA. Its entry in the unit-length axis for PC1 is 0.6, and PC1 has eigenvalue 2.25. What is the feature's loading on PC1 in the correlation sense, and what does it tell you?
Show answer
The loading is 0.6 × sqrt(2.25) = 0.6 × 1.5 = 0.9. The feature has a correlation of about 0.9 with the PC1 scores, so PC1 tracks this feature very closely.
Exercise
You refit PCA after adding 20 new rows. Component 1 now has the opposite sign on every loading, but the explained variance ratio is the same. Is something wrong?
Show answer
No. The sign of an eigenvector is arbitrary, so flipping all its entries (and all its scores) describes the same direction. The interpretation is unchanged: features that shared a sign still share one, and features with opposite signs still trade off. Only a change in the relative pattern, or in the explained variance, would signal a real difference.