Module 7 — Dimensionality Reduction: PCA
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
Rotate the projection line using the slider below. The bar reports the fraction of the total variance preserved by the projection. Identify the angle at which this quantity is maximized — that direction is, by definition, PC1. Click Snap to PC1 to display the value computed analytically by PCA from the data's 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.