Skip to content
Lambdian

Principal component analysis

Rotate to the directions of greatest variance and keep the few that matter.

Animated explanation

The idea

Data with many features is often secretly low-dimensional: height and arm span move together; pixel values in a face image are highly correlated. PCA finds new axes, the principal components, ordered by how much of the data's variance lies along them. The first points along the direction the data is most spread out; the second is the most spread direction perpendicular to it; and so on.

Keep the first few components and drop the rest: you have compressed the data while losing as little variance (information, in a squared-error sense) as possible. It is used to visualise high-dimensional data, to denoise, to speed up other models, and to remove multicollinearity.

Two equivalent views

Maximum variance: the first component maximises the variance of the projections w⊤x\mathbf{w}^\top\mathbf{x}. Minimum reconstruction error: it minimises the squared distance between points and their projections onto the line. By Pythagoras these are the same problem. The solution is the eigenvectors of the covariance matrix, computed stably with the SVD of the centred data.

Using PCA well

  • Centre the data (PCA assumes zero mean) and usually standardise it, or features with large units dominate.
  • Fit on training data only, then transform validation and test data with the same components.
  • Components are linear combinations of all features, so they are harder to interpret. Look at the loadings.
  • PCA only captures linear structure and variance. Class-separating directions can have low variance (use LDA), and curved manifolds need kernel PCA, t-SNE/UMAP or autoencoders.

The algorithm, step by step

  1. 1
    Standardise the features and centre them: Xc=X−xˉX_c = X - \bar{\mathbf{x}}.
  2. 2
    Compute the SVD Xc=USV⊤X_c = USV^\top (or eigen-decompose the covariance).
  3. 3
    Explained variance of component jj: sj2/(n−1)s_j^2/(n-1); choose kk from the cumulative ratio.
  4. 4
    Project: Z=XcVkZ = X_cV_k. Reconstruct: X^=ZVk⊤+xˉ\hat X = ZV_k^\top + \bar{\mathbf{x}}.

The maths

Covariance

Σ=1n−1Xc⊤Xc\Sigma = \frac{1}{n-1}X_c^\top X_c

Eigenproblem

Σvj=λjvj,λ1≥λ2≥…\Sigma\mathbf{v}_j = \lambda_j\mathbf{v}_j,\quad \lambda_1\ge\lambda_2\ge\dots

Projection and reconstruction

Z=XcVk,X^c=ZVk⊤Z = X_cV_k,\qquad \hat X_c = ZV_k^\top

Reconstruction error

∥Xc−X^c∥F2=(n−1)∑j>kλj\lVert X_c - \hat X_c\rVert_F^2 = (n-1)\sum_{j>k}\lambda_j

Strengths and weaknesses

Strengths

  • Fast, deterministic, no hyperparameters except k
  • Optimal linear compression in squared error
  • Decorrelates features; denoises; speeds up downstream models

Weaknesses

  • Linear only
  • Sensitive to feature scaling and outliers
  • Components can be hard to interpret; variance ≠ usefulness

Reach for it when…

Many correlated numeric features: visualisation, compression, noise reduction, or preprocessing before clustering or regression.

Check your understanding

Question 1

Q1The first principal component is…

Question 2

Q2Why standardise before PCA?

Question 3

Q3Eigenvalues 6, 3, 0.6, 0.4. What fraction of variance do two components keep?

Lambdian Lab

PCA by SVD

Implement PCA.

  • fit_pca(X, k): centre X, take the SVD, return (mean, components (k × d), explained_variance (k), explained_variance_ratio (k)). Use variance with denominator n − 1.
  • transform(X, mean, components) and inverse_transform(Z, mean, components).
  • n_components_for(X, threshold): the smallest k whose cumulative explained ratio reaches the threshold.
Open the labPython + NumPy + scikit-learn runs in your browser. Sign-in keeps your code and attempts.