ML0073 Principal Component Analysis

How does Principal Component Analysis (PCA) work, step by step?

Answer

PCA finds the orthogonal directions of maximum variance in centered data and projects the data onto the top k of them. Step by step: center each feature by subtracting its mean (and scale to unit variance when units differ); compute the covariance matrix; eigendecompose it, or equivalently run SVD on the data matrix, so eigenvectors become the principal directions and eigenvalues the variance each direction carries; keep the top k eigenvectors and project every sample onto them. The result is a rotated coordinate system whose axes are uncorrelated and ordered by explanatory power, which is why the first few components often carry most of the variance.

(1) Variance as Information: each principal component is the direction of maximum residual variance, orthogonal to all previous ones, and its eigenvalue \lambda_j is exactly the variance the data has along it.
(2) Explained-Variance Ratio Picks k: plot the cumulative share \sum_{j \leq k} \lambda_j / \sum_j \lambda_j and keep enough components to cover, say, 95% of total variance.
(3) Linear and Scale-Sensitive: PCA rotates but never unrolls, so curved manifolds need non-linear methods, and features with different units must be standardized first. Learned embeddings sidestep truncation differently: OpenAI’s text-embedding-3 models were trained with Matryoshka representation learning, so shortening a 3072-dimension vector to 256 still beats the older full-size ada-002, a property naive PCA truncation of a learned embedding cannot promise.

Two-dimensional elliptical point cloud with the first principal component arrow along the major axis and the second along the minor axis

Figure 1: The two principal components of a 2D cloud: PC1 lies along the direction of maximum spread and carries the largest eigenvalue; PC2 is orthogonal to it and mops up the residual variance.

Mathematical Formulation:
\Sigma = \frac{1}{N} X^{\top} X \ \ (\text{centered } X)
\Sigma v_j = \lambda_j v_j
z_i = V_k^{\top} x_i

Where:

  • X is the centered data matrix with N samples and d features; \Sigma is its d \times d covariance matrix.
  • v_j, \lambda_j are the eigenvector-eigenvalue pairs of \Sigma, sorted by descending \lambda_j.
  • V_k stacks the top k eigenvectors and z_i is the k-dimensional score vector of sample x_i.
Scree plot: bars of per-component variance falling off, with a cumulative explained variance line crossing the 95 percent mark

Figure 2: The scree plot: bars show the variance each component carries and the curve shows the cumulative share. Keeping components up to the 95% crossing is the standard rule for choosing k.

Projection is lossy: the discarded components carry the remaining variance, so projecting back into the original space reconstructs the data only up to the kept subspace. In 2D this means every point collapses onto the PC1 line when only one component is kept, which is the geometric picture behind “95% of variance retained”.

Point cloud with each point connected by a thin line to its perpendicular reconstruction on the principal component axis

Figure 3: Reconstruction from one component: every point is replaced by its perpendicular projection onto the PC1 axis. The gray connectors are the reconstruction errors, whose mean squared length is exactly the discarded variance \lambda_2 (their sum is N \lambda_2).

FeatureEigendecomposition of CovarianceSVD of the Data Matrix
InputThe d \times d matrix \SigmaThe centered N \times d matrix X directly
Numerical StabilityForming X^{\top} X squares the condition numberStable: never materializes X^{\top} X
ScalabilityFine for small dRandomized SVD scales to huge N and d
PracticeTextbook derivationWhat libraries (scikit-learn) actually call

Login to view more content


Log in to track your progress

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *