Equations · Pearson (1901); Hotelling (1933)

Principal component analysis (PCA), explained term by term

PCA finds the directions along which centered data vary the most. The first principal component is the unit direction of largest variance; each next one is the largest-variance direction orthogonal to those before. They are the eigenvectors of the covariance matrix, and projecting onto the first kk reduces the data to kk dimensions while keeping as much variance as possible.

w1=arg max⁡∥w∥2=1w⊤S w,S=1n−1X⊤X=VΛV⊤,Z=XVk\mathbf{w}_1 = \argmax_{\|\mathbf{w}\|_2 = 1} \mathbf{w}^\top S\, \mathbf{w}, \quad S = \frac{1}{n-1} X^\top X = V \Lambda V^\top, \quad Z = X V_k

Open in the decoder →

Term by term

  1. XX

    The data matrix, one row per example, centered: each column's mean has been subtracted. Forgetting to center is the classic PCA bug.

  2. S=1n−1X⊤XS = \frac{1}{n-1} X^\top X

    The sample covariance matrix of the nn examples: d×dd \times d, symmetric.

  3. arg max⁡∥w∥2=1w⊤S w\argmax_{\|\mathbf{w}\|_2 = 1} \mathbf{w}^\top S\, \mathbf{w}

    The unit vector w\mathbf{w} that maximizes w⊤Sw\mathbf{w}^\top S \mathbf{w}, the variance of the data projected onto w\mathbf{w}. The unit-length constraint stops w\mathbf{w} from growing without bound.

  4. VΛV⊤V \Lambda V^\top

    The eigendecomposition of SS: the columns of VV are the principal directions, and the diagonal of Λ\Lambda holds the variances along them, λ1≥λ2≥…\lambda_1 \ge \lambda_2 \ge \ldots.

  5. Z=XVkZ = X V_k

    The projected data: each example's coordinates along the first kk components. VkV_k is the first kk columns of VV. The same directions come from the SVD of XX: they are its right singular vectors, with λi=σi2/(n−1)\lambda_i = \sigma_i^2/(n-1).

Shapes

A small worked example

Centered points (−2,−1),(0,0),(2,1)(-2, -1), (0, 0), (2, 1): X⊤X=(8442)X^\top X = \begin{pmatrix} 8 & 4 \\ 4 & 2 \end{pmatrix}, so S=(4221)S = \begin{pmatrix} 4 & 2 \\ 2 & 1 \end{pmatrix}, with eigenvalues 5 and 0. The first component is w1=(2,1)/5\mathbf{w}_1 = (2, 1)/\sqrt{5}; the projections are (−2.24,0,2.24)(-2.24, 0, 2.24), with variance 5. One dimension keeps 100% of the variance, because the points lie on a line.

In code

import numpy as np

def pca(X, k):
    Xc = X - X.mean(axis=0)                           # center first
    U, S, Vt = np.linalg.svd(Xc, full_matrices=False)
    var = S**2 / (len(X) - 1)                         # eigenvalues of the covariance
    return Xc @ Vt[:k].T, var[:k]                     # Z (n, k), variances

Every symbol in it

W∈Rm×n\mathbf{W} \in \mathbb{R}^{m \times n}Bold uppercase (matrix)Cov⁡(X,Y)\operatorname{Cov}(X, Y)Covariancex⊤y\mathbf{x}^\top \mathbf{y}Transposeab\frac{a}{b}Fractionarg⁡max⁡θf(θ)\arg\max_{\theta} f(\theta)Argmax∥x∥2\|\mathbf{x}\|_2Normx∈Rd\mathbf{x} \in \mathbb{R}^{d}Bold lowercase (vector)λ∥w∥22\lambda \|\mathbf{w}\|_2^2Lambdadiag⁡(v)\operatorname{diag}(\mathbf{v})Diagxi,  Wq,  pθ,  dmodelx_i,\; W_q,\; p_\theta,\; d_{\text{model}}Subscript (index or label)

Other equations