Skip to content
mlmentorship

SVD and PCA

The singular value decomposition factorizes any matrix into rotation × stretching × rotation. PCA is SVD applied to mean-centered data.

Published · 4 min read ·Core ·Foundation

Visual quick review

Visual first · depth when needed

See rank-1 PCA as orthogonal projection onto the direction of greatest variance, with the discarded perpendicular residual becoming reconstruction error.

Preparing the visual…

Summary

Every real matrix admits the factorization where and are orthogonal and is diagonal with non-negative entries (singular values). PCA is SVD applied to a mean-centered data matrix.

SVD is the universal matrix factorization. It exists for every matrix, even rectangular and rank-deficient ones. Reading off properties from the SVD answers “what does this matrix do?”: singular values give scaling factors, gives input directions, gives output directions.

PCA is the canonical use of SVD: project data onto the directions of largest variance to get a low-dimensional representation that preserves as much information as possible.

The decomposition

:

  • ‘s columns are orthonormal input directions; the first span the row space and the rest span the nullspace.
  • ’s columns are orthonormal output directions; the first span the column space and the rest span the left nullspace.
  • ’s diagonal entries are the singular values (how much each input direction is stretched into its output direction).

Geometrically: any linear map is “rotate the input, stretch axis-by-axis, rotate the output.” That’s it.

The rank of is the number of non-zero singular values. For a full-rank map, the 2-norm condition number is . A rank-deficient map has infinite condition number; instead describes conditioning after restricting to its rank- subspace.

Truncated SVD and low-rank approximation

The best rank- approximation of in Frobenius (or spectral) norm is

where keep the first columns and keeps the first singular values (Eckart–Young theorem). Used in: dimensionality reduction, image compression, embedding regularization, low-rank LoRA fine-tuning.

PCA as SVD

Given a data matrix ( samples, features):

  1. Mean-center: .
  2. Compute SVD: .
  3. The columns of are the principal components (directions of maximum variance in feature space).
  4. The variance along the -th component is .
  5. Project to dimensions: .

Equivalent formulation: PCA = eigendecomposition of the sample covariance . SVD is numerically more stable.

Spatial intuition

Rank-1 PCA keeps position along PC1 and discards the perpendicular residual.

Six centered samples projected onto their first principal component Six circular sample points form a long, narrow cloud around the mean. PC1 follows the cloud's longest direction and PC2 is perpendicular. Dashed perpendicular segments connect every sample to a diamond on PC1. Each diamond is that sample's rank-1 reconstruction; each dashed segment is the discarded residual. centered sample x rank-1 reconstruction x̂ PC1: greatest variance PC2: discarded mean = 0 perpendicular residual
Read it this way: each circle drops perpendicularly to a diamond on PC1. The diamond keeps the coordinate with the largest spread; the dashed segment is the discarded PC2 coordinate and therefore the rank-1 reconstruction error.

Common pitfalls

  • Forgetting to center. PCA on uncentered data finds the direction toward the mean as PC1, which is rarely what you want.
  • Forgetting to scale. If features have different units, large-magnitude features dominate; standardize (divide by std) before PCA when units differ.
  • Confusing PCA with whitening. PCA gives uncorrelated components but not unit variance. Whitening = PCA + scale to unit variance.
  • Using PCA on categorical / sparse data without thought. PCA assumes Euclidean structure; for sparse / categorical data, look at NMF, LDA, or contrastive embeddings.