Curriculum03. Unsupervised LearningLesson 4 of 4

Stage 10: PCA & Dimensionality Reduction

Spectral decomposition of feature covariance into orthogonal variance axes.

26 min readAdvancedModule 03
PHASE 01 // HOOK

The Shadow on the Wall

When you cast a shadow of your 3D hand onto a flat 2D wall, you lose one dimension of information. How can you orient your hand so that the shadow retains the maximum amount of detail and recognizable structure?

PHASE 02 // VISUAL INTUITION

Centering & Variance Maximization

PCA begins by shifting the data so its mean is at the origin (0,0)(0, 0). It then rotates the coordinate axes to align with the directions of maximum spread (variance), discarding axes with minimal information.

PHASE 03 // EXPLORE

Interactive Covariance & Eigenvector Lab

Inspect the blue principal component vector v1\mathbf{v}_1. Red dotted lines show the orthogonal projection error discarded when compressing 2D points onto the line.

PHASE 04 // PREDICT

Predicting Reconstruction Error

If all data points lie on a perfectly straight diagonal line (y=2xy = 2x), what percentage of variance is captured by the first principal component PC1?

Algorithmic Checkpoint

What is the explained variance ratio of PC1 when data is perfectly collinear?

PHASE 05 // REVEAL

The Covariance Matrix & Spectral Theorem

The sample covariance matrix C=1m−1XcTXc\mathbf{C} = \frac{1}{m-1} \mathbf{X}_c^T \mathbf{X}_c is symmetric and positive semi-definite. By the Spectral Theorem, it has real eigenvalues and orthogonal eigenvectors that form a new coordinate basis.

PHASE 06 // UNDER THE HOOD

Mathematical Formulation

Eigenvector v1\mathbf{v}_1 defines the direction of the first principal axis. The corresponding eigenvalue λ1\lambda_1 equals the exact variance of the projected data points along that axis.

PRIMARY OBJECTIVE FORMULATION
PHASE 07 // BREAK IT

Break The Algorithm: The Swiss Roll & Label Blindness

Because PCA is strictly linear and unsupervised, it fails on non-linear manifolds (like a spiral Swiss Roll) and can accidentally discard low-variance dimensions that contain crucial classification boundaries.

PHASE 08 // CODE IT

Scikit-Learn Implementation

Compute PCA projection and variance ratios in Python.

Production Implementation
import numpy as np

# 1. Center data
X_c = X - np.mean(X, axis=0)
# 2. Covariance matrix
C = np.cov(X_c, rowvar=False)
# 3. Eigen decomposition
eigenvalues, eigenvectors = np.linalg.eigh(C)
Implementation Walkthrough:

Scikit-Learn uses SVD (Singular Value Decomposition) directly on X_c rather than computing X_c^T X_c to maintain numerical precision and avoid squaring condition numbers.

PHASE 09 // ASSESSMENT

Diagnostic Understanding Check

Test your understanding of spectral dimensionality reduction.

PHASE 10 // MASTERY

Mastery & Synthesis

You have mastered data centering, covariance matrices, and spectral projections. You have completed the 10 Essential Core Algorithm Stages in ML in Motion! Next, advance into Stage 11: Model Evaluation & Generalization to validate your models rigorously.