Module 7 — Dimensionality Reduction: PCA
Focus questions
Keep these questions in mind as you work through the module. Answer them in your guided notes, and use them to decide what belongs on your reference card.
- In your own words, why does compressing an elongated, rotated two-dimensional cloud onto one line lose little information, and what is discarded along the orthogonal direction?
- Using the rotation activity, how would you find the projection angle that maximizes retained variance, and how would you interpret an explained_variance_ratio_ value such as the one the program prints?
- Under what distribution of variance between the principal components would discarding PC2 lose important structure, and how would you decide how many components to keep?
Real-world datasets frequently contain hundreds of features — far more than can be directly visualized or interpreted. Dimensionality reduction projects high-dimensional data onto a small number of derived features while preserving as much of the data's structure as possible. The classical technique is Principal Component Analysis (PCA), which identifies the orthogonal directions of greatest variance in the data and retains the leading few.
The central principle: variance as information
Consider an elongated, rotated cloud of points. The variance along its principal axis is large; the variance along the orthogonal axis is small. PCA identifies these axes — the principal components — ordered by the variance they account for:
- PC1 — the direction of maximum variance (the principal axis of the cloud).
- PC2 — the direction orthogonal to PC1 that captures the maximum remaining variance.
When the great majority of the variance is concentrated along PC1, PC2 can be discarded and each observation represented by a single coordinate — its projection onto PC1 — with minimal loss of information. This is the essence of dimensionality reduction: two features are compressed into one with negligible loss of structure.
Orthogonal projection onto a line
Reducing the data to one dimension is accomplished by orthogonally projecting each observation onto a single line — dropping the point perpendicularly onto the line and recording its position along it. The fraction of the original variance preserved by the projection is the retained variance. The projection onto PC1 maximizes the retained variance; projection onto any other direction necessarily preserves less.
Locate the principal axis interactively
Two axes are drawn together: the green line is the single dimension you keep, and the violet dashed line is the orthogonal second dimension — the information a projection discards. Rotate the green line with the slider, then press Project → 1-D to watch every point slide down its perpendicular onto the line: two coordinates collapse into one, and the violet residuals show exactly what is lost. The bar reports the fraction of the total variance the projection keeps; the readout also reports how much the second dimension drops. Find the angle that maximizes the retained variance — that direction is, by definition, PC1 — or click Snap to PC1 for the value PCA computes analytically from the covariance matrix.
This activity needs JavaScript. The lesson below still covers everything.
from sklearn.decomposition import PCA pca = PCA(n_components=1) # keep just the strongest direction Z = pca.fit_transform(X) # each point → its position on PC1 pca.explained_variance_ratio_ # fraction of variance kept, e.g. [0.92]import numpy as np import matplotlib.pyplot as plt from sklearn.decomposition import PCA # A correlated 2-D cloud: most of its variance lies along one diagonal rng = np.random.default_rng(0) t = rng.normal(0, 1, size=200) X = np.c_[t * 2.0, t * 1.0] + rng.normal(0, 0.3, size=(200, 2)) pca = PCA(n_components=1) Z = pca.fit_transform(X) # each point → its position on PC1 recon = pca.inverse_transform(Z) # put it back in 2-D, on the line kept = pca.explained_variance_ratio_[0] print("explained_variance_ratio_ =", np.round(pca.explained_variance_ratio_, 3)) print(f"kept {kept*100:.1f}% of the variance using 1 of 2 dimensions") plt.figure(figsize=(5, 3.4)) plt.scatter(X[:, 0], X[:, 1], s=12, alpha=0.4, label="original 2-D") plt.scatter(recon[:, 0], recon[:, 1], s=12, color="crimson", label="projected to PC1") plt.axis("equal"); plt.legend(); plt.title("PCA: 2-D → 1-D"); plt.tight_layout(); plt.show()
explained_variance_ratio_ corresponds precisely to the "retained variance" indicator in the activity above — the fraction of the data's variance preserved after the reduction. Click Run it yourself to observe how little variance is lost when the data are intrinsically low-rank, i.e. when most of the variance is concentrated along a single direction.
When you run it, the program prints (and draws a chart):
explained_variance_ratio_ = [0.981] kept 98.1% of the variance using 1 of 2 dimensions
Check your understanding
Answer a short set of questions on principal components, variance, and projection.
This activity needs JavaScript.