PCA (Principal Component Analysis) Explained: Variance, Eigenvectors & Scree Plots
The intuitive beginner's guide to PCA — rotating data onto maximum-variance axes, reading Explained Variance Ratio (Scree plots), and avoiding PCA pitfalls
Principal Component Analysis (PCA) is an unsupervised linear technique that rotates a high-dimensional dataset onto new orthogonal axes (Principal Components) ordered from highest variance to lowest, letting you keep the top K axes and drop noisy dimensions.
Datasets with 100+ correlated columns are slow to train, suffer from multicollinearity, and are impossible to plot — PCA compresses 95% of the information into a fraction of the dimensions.
PCA is the cornerstone of linear dimensionality reduction, noise filtering, multicollinearity removal, and high-dimensional visualization.
Compressing 1,536-dim embeddings down to 256 dims with PCA cuts vector database RAM and latency by 6× with minimal recall loss.
PCA eliminates multicollinearity completely (every Principal Component has VIF = 1!) and lets you visualize 50-feature customer tables in 2D.
Favorite question: 'Does PCA use target labels y? Can discarding the lowest-variance principal component EVER hurt a supervised classifier?'
Does PCA ever look at the target label y when choosing which directions to keep?
- Trace the exact 4-step PCA recipe: Standardize → Covariance/SVD → Top-K Eigenvectors → Project.
- Read a Scree Plot and Cumulative Explained Variance curve to choose K (e.g., keeping 95% variance).
- Explain why Standardizing BEFORE PCA is mandatory whenever features have different units.
- Know when NOT to use PCA (when you need direct column-name interpretability, or when low-variance directions actually separate the target y!).
Imagine a slanted cigar-shaped cloud of dots in 3D. Almost all the information lies along the cigar's length (PC1) and width (PC2); the tiny thickness (PC3) is mostly noise. PCA rotates your coordinate system to align with the cigar so you can safely drop PC3!
1. The 4-Step PCA Recipe
- Standardize every feature () strictly on
X_trainso large-unit columns don't hijack PC1. - Compute SVD () — numerically faster and more accurate than forming .
- Pick using the Cumulative Explained Variance curve (commonly 90%–95%, or for 2D plots).
- Project
Z_train = X_train @ V_KandZ_test = X_test @ V_K.
Rotating Axis θ = 0° — Watch Shadows Spread & Error Drops Shrink!
The exact honest questions almost every beginner wonders — answered in plain English without hiding behind jargon.
Loss of interpretability: Each component is a linear blend of ALL original features (PC1 = 0.31·age − 0.52·income + ...), so you can no longer say 'age drove this prediction' directly.
Choose Top-k Principal Components (Out of d = 10 Features)
import numpy as np
# Exact PCA via SVD From Scratch + Explained Variance Ratio
rng = np.random.default_rng(0)
t = rng.normal(size=100)
X = np.column_stack([2.0 * t + rng.normal(0, 0.2, 100), 1.0 * t + rng.normal(0, 0.2, 100), rng.normal(0, 0.1, 100)])
# 1. Standardize
X_std = (X - X.mean(axis=0)) / X.std(axis=0)
# 2. SVD -> singular values S give eigenvalues S^2 / (N - 1)
U, S, Vt = np.linalg.svd(X_std, full_matrices=False)
evr = (S ** 2) / np.sum(S ** 2)
# 3. Project onto top K=2 principal components
Z = X_std @ Vt[:2].T
print("Explained Variance Ratios:", np.round(evr, 3).tolist())
print(f"Top-2 cumulative variance: {evr[:2].sum():.1%} | Projected shape: {Z.shape}")
StandardScaler + PCA inside a Pipeline strictly on X_train!age (20–70) and salary (200k)?