2.5.8Unsupervised Learning

Principal Component Analysis (PCA) theory

2,492 words11 min readdifficulty · medium6 backlinks

What PCA Actually Does

Principal Component Analysis transforms high-dimensional data into a new coordinate system where:

  1. The first axis (1st principal component) points in the direction of maximum variance
  2. The second axis (2nd PC) points in the direction of maximum remaining variance, orthogonal to the first
  3. Each subsequent axis maximizes variance orthogonal to all previous ones

Why orthogonal? We want components to capture independent sources of variation. Orthogonality ensures no redundancy—each PC adds new information.

The Mathematics: Deriving PCA from Scratch

Step 1: Center the Data (WHY)

Given data matrix XRn×d\mathbf{X} \in \mathbb{R}^{n \times d} (nn samples, dd features), first center it: Xc=X1nμT\mathbf{X}_c = \mathbf{X} - \mathbf{1}_n \boldsymbol{\mu}^T where μ=1ni=1nxi\boldsymbol{\mu} = \frac{1}{n}\sum_{i=1}^n \mathbf{x}_i is the mean vector.

Why center? PCA finds directions through the data's "center of mass". Without centering, the first PC might just point toward the mean instead of capturing variance structure. Centering makes the coordinate origin the data's centroid.

Step 2: Formulate the Variance Maximization Problem

We want to find a unit vector w1\mathbf{w}_1 (first PC) such that projecting data onto it maximizes variance.

Projection of xi\mathbf{x}_i onto w1\mathbf{w}_1: zi=w1Txiz_i = \mathbf{w}_1^T \mathbf{x}_i

Variance of projections: Var(z)=1ni=1n(zizˉ)2\text{Var}(z) = \frac{1}{n}\sum_{i=1}^n (z_i - \bar{z})^2

Since data is centered, zˉ=0\bar{z} = 0: Var(z)=1ni=1n(w1Txi)2=1ni=1nw1TxixiTw1\text{Var}(z) = \frac{1}{n}\sum_{i=1}^n (\mathbf{w}_1^T \mathbf{x}_i)^2 = \frac{1}{n}\sum_{i=1}^n \mathbf{w}_1^T \mathbf{x}_i \mathbf{x}_i^T \mathbf{w}_1

Why this step? We're expanding the squared scalar projection. Using the property (aTb)2=aTbbTa(\mathbf{a}^T\mathbf{b})^2 = \mathbf{a}^T\mathbf{b}\mathbf{b}^T\mathbf{a} when the result is scalar.

Rearranging: Var(z)=w1T(1ni=1nxixiT)w1=w1TCw1\text{Var}(z) = \mathbf{w}_1^T \left(\frac{1}{n}\sum_{i=1}^n \mathbf{x}_i \mathbf{x}_i^T\right) \mathbf{w}_1 = \mathbf{w}_1^T \mathbf{C} \mathbf{w}_1

where C=1nXcTXc\mathbf{C} = \frac{1}{n}\mathbf{X}_c^T\mathbf{X}_c is the covariance matrix.

Step 3: Solve with Lagrange Multipliers

We want: maxw1w1TCw1subject tow1=1\max_{\mathbf{w}_1} \mathbf{w}_1^T \mathbf{C} \mathbf{w}_1 \quad \text{subject to} \quad \|\mathbf{w}_1\| = 1

Why the constraint? Without it, we could make variance arbitrarily large by scaling w1\mathbf{w}_1. The unit constraint means we're finding a direction, not a magnitude.

Lagrangian: L=w1TCw1λ(w1Tw11)\mathcal{L} = \mathbf{w}_1^T \mathbf{C} \mathbf{w}_1 - \lambda(\mathbf{w}_1^T \mathbf{w}_1 - 1)

Taking derivative with respect to w1\mathbf{w}_1 and setting to zero: Lw1=2Cw12λw1=0\frac{\partial \mathcal{L}}{\partial \mathbf{w}_1} = 2\mathbf{C}\mathbf{w}_1 - 2\lambda\mathbf{w}_1 = 0

Why derivative? We're finding critical points. The factor of 2 appears from differentiating quadratic forms: w(wTAw)=2Aw\frac{\partial}{\partial \mathbf{w}}(\mathbf{w}^T\mathbf{A}\mathbf{w}) = 2\mathbf{A}\mathbf{w} when A\mathbf{A} is symmetric.

This gives: Cw1=λw1\mathbf{C}\mathbf{w}_1 = \lambda \mathbf{w}_1

This is the eigen-equation! w1\mathbf{w}_1 must be an eigenvector of C\mathbf{C} with eigenvalue λ\lambda.

Which eigenvector? Substituting back into our objective: w1TCw1=w1T(λw1)=λw1Tw1=λ\mathbf{w}_1^T \mathbf{C} \mathbf{w}_1 = \mathbf{w}_1^T (\lambda \mathbf{w}_1) = \lambda \mathbf{w}_1^T \mathbf{w}_1= \lambda

The variance captured equals λ\lambda! To maximize variance, choose the eigenvector with the largest eigenvalue.

Why Subsequent Components Are Orthogonal

For the second PC w2\mathbf{w}_2, we maximize variance subject to:

  • w2=1\|\mathbf{w}_2\| = 1 (unit length)
  • w2Tw1=0\mathbf{w}_2^T \mathbf{w}_1 = 0 (orthogonal to first PC)

Mathematical reason: Eigenvectors of a symmetric matrix (like covariance matrices) corresponding to distinct eigenvalues are automatically orthogonal. Since covariance matrices are symmetric real matrices, they have a complete set of orthonormal eigenvectors.

Intuitive reason: If w2\mathbf{w}_2 had any component along w1\mathbf{w}_1, that component would capture variance already captured by w1\mathbf{w}_1 (redundant). Orthogonality ensures each PC captures new, uncorrelated variance.

Reconstruction and Dimensionality Reduction

Forward Transform (Encoding)

Project data onto first kk PCs: Z=XcWkRn×k\mathbf{Z} = \mathbf{X}_c \mathbf{W}_k \in \mathbb{R}^{n \times k}

Each row zi\mathbf{z}_i is the k-dimensional representation of sample xi\mathbf{x}_i.

Reconstruction (Decoding)

Approximate original data from reduced representation: X^c=ZWkT=XcWkWkT\hat{\mathbf{X}}_c = \mathbf{Z} \mathbf{W}_k^T = \mathbf{X}_c \mathbf{W}_k\mathbf{W}_k^T

Then add back the mean: X^=X^c+1nμT\hat{\mathbf{X}} = \hat{\mathbf{X}}_c + \mathbf{1}_n\boldsymbol{\mu}^T

Reconstruction error (mean squared error): MSE=1nXcX^cF2=i=k+1dλi\text{MSE} = \frac{1}{n}\|\mathbf{X}_c - \hat{\mathbf{X}}_c\|_F^2 = \sum_{i=k+1}^d \lambda_i

Why this formula? The Frobenius norm squared equals the sum of eigenvalues we didn't include. Each discarded PC contributes its eigenvalue to the error.

Mathematical Properties

1. PCA Minimizes Reconstruction Error

PCA with kk components gives the best rank-k approximation of Xc\mathbf{X}_c under the Frobenius norm: Wk=argminW:WTW=IkXcXcWWTF2\mathbf{W}_k = \arg\min_{\mathbf{W}: \mathbf{W}^T\mathbf{W}=\mathbf{I}_k} \|\mathbf{X}_c - \mathbf{X}_c\mathbf{W}\mathbf{W}^T\|_F^2

Why? This is the Eckart-Young theorem. The best low-rank approximation comes from the SVD/eigendecomposition.

2. PCA via Singular Value Decomposition

Alternative computation: SVD of centered data: Xc=UΣVT\mathbf{X}_c = \mathbf{U}\mathbf{\Sigma}\mathbf{V}^T

Then:

  • Principal components: W=V\mathbf{W} = \mathbf{V} (right singular vectors)
  • Eigenvalues: λi=σi2n\lambda_i = \frac{\sigma_i^2}{n} (squared singular values divided by nn)
  • Projected data: Z=UΣ=XcV\mathbf{Z} = \mathbf{U}\mathbf{\Sigma} = \mathbf{X}_c\mathbf{V}

Why SVD? More numerically stable than computing XcTXc\mathbf{X}_c^T\mathbf{X}_c explicitly, especially when dnd \gg n.

3. Covariance in PC Space

After projection, covariance of Z\mathbf{Z} is diagonal: Cov(Z)=1nZTZ=Λk=diag(λ1,,λk)\text{Cov}(\mathbf{Z}) = \frac{1}{n}\mathbf{Z}^T\mathbf{Z} = \mathbf{\Lambda}_k = \text{diag}(\lambda_1, \ldots, \lambda_k)

Meaning: Principal components are uncorrelated. We've decorelated the data.

Recall Explain to a 12-Year-Old

Imagine your class takes a photo together. You have everyone's position in3D space (left-right, forward-back, up-down). But when you look at the photo, you realize most of the information is in just 2 dimensions—the photo plane! The 3rd dimension (depth) barely matters because everyone was standing at roughly the same distance from the camera.

PCA is like finding the "photo angle" for data. It looks at your data cloud and asks: "If I had to take a picture (project onto fewer dimensions), which angle keeps the most information?" The first principal component is like the camera's main direction—it captures where people are most spread out. The second PC is like looking from the side to catch any remaining spread.

Why do we want fewer dimensions? Imagine describing everyone's position. Instead of saying "3.2m left, 1.8m forward, 0.1m higher than Sarah," you could just say "Row 3, Position 7" if everyone's standing in neat rows. Two numbers instead of three! PCA finds those "neat rows" in mesy data.

Connections

  • Covariance Matrix — PCA decomposes this to find variance structure
  • Eigendecomposition — The mathematical tool underlying PCA
  • Singular Value Decomposition — Alternative (more stable) way to compute PCA
  • Linear Autoencoders — Neural networks that learn PCA-like representations when linear
  • Kernel PCA — Nonlinear extension using the kernel trick
  • Factor Analysis — Probabilistic model related to PCA with explicit noise
  • Independent Component Analysis — Finds statistically independent (not just uncorrelated) components
  • Feature Scaling — Necessary preprocessing for PCA with mixed units
  • Dimensionality Reduction — PCA is one technique; compare with t-SNE, UMAP
  • Variance — The quantity PCA maximizes in successive orthogonal directions

#flashcards/ai-ml

What is the goal of PCA? :: Find orthogonal directions that maximize variance in the data, enabling dimensionality reduction while preserving the most information.

Why must we center data before PCA?
So the first PC captures variance structure rather than just pointing toward the mean; PCA seeks variance around the centroid, not from the origin.
What is a principal component mathematically?
An eigenvector of the covariance matrix; the k-th PC is the eigenvector corresponding to the k-th largest eigenvalue.
What does the eigenvalue represent in PCA?
The variance captured by the corresponding principal component; larger eigenvalue = more variance explained.
How do we compute the covariance matrix for PCA?
C=1nXcTXc\mathbf{C} = \frac{1}{n}\mathbf{X}_c^T\mathbf{X}_c where Xc\mathbf{X}_c is the centered data matrix (rows are samples, columns are features).
Why are principal components orthogonal?
(1) Eigenvectors of symmetric matrices with distinct eigenvalues are orthogonal. (2) Ensures each PC captures independent variance, no redundancy.
How do we project data onto the first k principal components?
Z=XcWk\mathbf{Z} = \mathbf{X}_c \mathbf{W}_k where Wk\mathbf{W}_k contains the first k eigenvectors as columns.
What is the reconstruction formula in PCA?
X^c=ZWkT=XcWkWkT\hat{\mathbf{X}}_c = \mathbf{Z}\mathbf{W}_k^T = \mathbf{X}_c\mathbf{W}_k\mathbf{W}_k^T, then add back mean: X^=X^c+1nμT\hat{\mathbf{X}} = \hat{\mathbf{X}}_c + \mathbf{1}_n\boldsymbol{\mu}^T.
What is the reconstruction error when keeping k components?
MSE=i=k+1dλi\text{MSE} = \sum_{i=k+1}^d \lambda_i — the sum of discarded eigenvalues equals the squared error.
How much variance do k components explain?
i=1kλii=1dλi\frac{\sum_{i=1}^k \lambda_i}{\sum_{i=1}^d \lambda_i} — ratio of sum of kept eigenvalues to total sum.
Is PCA feature selection or feature extraction?
Feature extraction; it creates new features (linear combinations of all originals), not selecting a subset.
Why standardize features before PCA?
Because PCA is not scale-invariant; features with larger numeric scales dominate variance. Standardizing puts all features on equal footing.
What is the difference between covariance-based and correlation-based PCA?
Covariance PCA uses raw data; correlation PCA uses standardized data (equivalent to computing eigenvectors of correlation matrix). Use correlation when features have different units.
How does SVD relate to PCA?
Xc=UΣVT\mathbf{X}_c = \mathbf{U\Sigma V}^T; principal components are right singular vectors V\mathbf{V}, eigenvalues are λi=σi2/n\lambda_i = \sigma_i^2/n. More numerically stable.
What is the covariance structure in PC space?
Diagonal — Cov(Z)=Λk\text{Cov}(\mathbf{Z}) = \mathbf{\Lambda}_k. Principal components are uncorrelated by construction.

Concept Map

center by mean

compute

of projections

variance equals

constrained w unit norm

yields

solutions are

eigen-decomposition

ordered by eigenvalue

orthogonal for

keep top k

no redundancy

High-dim data X

Centered data Xc

Covariance matrix C

Maximize variance

Projection z = wTx

wT C w

Lagrangian optimization

Eigenvector equation C w = lambda w

Principal components

Eigenvalue lambda equals variance

Independent variation

Dimensionality reduction

Hinglish (regional understanding)

Intuition Hinglish mein samjho

PCA ka matlab hai Principal Component Analysis – yek technique hai jisse hum high-dimensional data ko kam dimensions mein convert kar sakte hain, lekin important information retain rahti hai. Socho ki tumhare pas 100 features hain (100 columns), lekin asli mein sirf 3-4 "directions" mein sab variation hai. PCA un important directions ko dhondh leta hai.

Mathematically, PCA covariance matrix ke eigenvectors nikalta hai. Covariance

Test yourself — Unsupervised Learning

Connections