Sub topic 13
Limitations and Common Pitfalls
This chapter assumes you can fit PCA with NumPy or scikit-learn, and that you know what centering, standardizing, explained variance and loadings mean. Here you will learn where PCA gives misleading results and how to catch those cases.
Scale decides the result
PCA maximizes variance, and variance depends on units. A feature measured in grams has a larger variance than the same feature measured in kilograms. Without standardizing, the first component tends to line up with whichever column has the largest numbers.
import numpy as np
from sklearn.decomposition import PCA
rng = np.random.default_rng(0)
a = rng.normal(size=300)
b = a + rng.normal(scale=0.5, size=300) # correlated with a
c = rng.normal(size=300) * 1000 # unrelated, huge scale
X = np.column_stack([a, b, c])
print(PCA(n_components=1).fit(X).components_)The printed component is close to [0, 0, 1]. It points at the noisy, large-scale column and ignores the real correlation between a and b. Standardizing first (StandardScaler) fixes this. Skip standardizing only when all features share one unit and the differences in scale carry meaning.
Variance is not importance
PCA never looks at a target. A direction with small variance can still be the one that separates your classes, and a high-variance direction can be pure noise or a nuisance factor such as lighting in images.
Only linear structure and only second moments
PCA finds straight directions and uses only means and covariances. If the data lies on a curve, such as a spiral or a ring, no single line captures it, and PCA needs many components to describe a one-dimensional curve. Uncorrelated also does not mean independent: components are uncorrelated, but they can still depend on each other in nonlinear ways.
Outliers and leakage
A single extreme point can pull the covariance matrix, and therefore the first component, toward itself. Inspect the data for outliers before fitting, or consider a robust variant.
A second pitfall is data leakage. Fit the scaler and PCA on the training split only, then apply them to the test split. The pipeline below does this correctly because fit sees only the training data.
from sklearn.datasets import load_wine
from sklearn.decomposition import PCA
from sklearn.linear_model import LogisticRegression
from sklearn.model_selection import train_test_split
from sklearn.pipeline import make_pipeline
from sklearn.preprocessing import StandardScaler
X, y = load_wine(return_X_y=True)
X_train, X_test, y_train, y_test = train_test_split(
X, y, test_size=0.3, random_state=0, stratify=y
)
model = make_pipeline(StandardScaler(), PCA(n_components=3),
LogisticRegression(max_iter=1000))
model.fit(X_train, y_train)
print(model.score(X_test, y_test))Interpretation traps
- Sign is arbitrary. An eigenvector and its negative describe the same axis. Different libraries or versions may flip signs, so never compare raw signs across runs.
- Close eigenvalues are unstable. If two eigenvalues are nearly equal, the individual axes are poorly determined. Only the plane they span is meaningful.
- Loadings mix features. Each component combines all features, so naming a component after one feature is often wrong.
- Missing values are not handled by PCA. Impute them first, and remember that the imputation affects the covariance.
- Fewer samples than features. The covariance matrix then has rank at most
n - 1, so you get at most that many non-zero components.
Quick diagnostic checklist
| Symptom | Likely cause | Remedy |
|---|---|---|
| First component matches one column | Unstandardized features | Use `StandardScaler` |
| Good variance kept, poor model accuracy | Signal sits in low-variance directions | Keep more components or skip PCA |
| Many components needed for a curved dataset | Nonlinear structure | Kernel PCA, t-SNE, UMAP |
| Test score much worse than expected | Fit on all data before splitting | Use a pipeline |
Exercise
You run PCA on a table with columns for income in dollars and age in years, without standardizing. The first component is almost entirely income. Why does this happen, and what is the fix?
Show answer
Income has a far larger variance than age because of its units, and PCA maximizes variance, so the leading direction follows income. Standardize each column to zero mean and unit variance (for example with StandardScaler) before fitting PCA.
Exercise
Why is it a mistake to fit PCA on the full dataset and then split into training and test sets?
Show answer
The components then use information from the test samples, which leaks into training and makes the test score too optimistic. Fit the scaler and PCA on the training split only and apply them to the test split, ideally inside a pipeline.