Singular Value Decomposition
SVD definition, low-rank approximation, connection to PCA
You work at a streaming service. Your rating matrix is 1 million users by 10,000 movies. Most entries are missing — users have rated a tiny fraction of the catalog. Your job is to predict the missing entries so you can recommend movies users have not seen yet.
The naive approach: fill in missing entries with the average rating. This ignores everything you know about which users are similar and which movies are similar. What you want is to find latent structure — users who like action movies, comedies, prestige dramas — and represent both users and movies in that latent space.
SVD gives you this. Factor the matrix $M \approx U Sigma V^T$. $U$ is a 1M × k matrix — each user represented as a $k$-dimensional latent preference vector. $V$ is a 10,000 × k matrix — each movie as a $k$-dimensional latent attribute vector. $Sigma$ is $k \times k$ diagonal — the importance of each latent factor. To predict user $i$'s rating of movie $j$: compute $u_i^T Sigma v_j$. The dot product measures how well user $i$'s preferences align with movie $j$'s attributes across all $k$ latent dimensions. This was the foundation of most Netflix Prize solutions.
Why not eigendecomposition instead? Eigendecomposition requires a square symmetric matrix. Your rating matrix is 1M × 10,000 — rectangular. SVD works on any matrix. The singular values $sigma_i = \sqrt{lambda_i(M^T M)}$ — they are the square roots of the eigenvalues of $M^T M$. Left singular vectors $U$ are eigenvectors of $MM^T$; right singular vectors $V$ are eigenvectors of $M^T M$. SVD is strictly more general.
Two SVD variants matter here, and the quiz below tests the difference. The full SVD of an m×n matrix keeps every dimension: U is m×m orthogonal, Σ is m×n (padded with zero rows or columns beyond the rank), V is n×n orthogonal — U and V are square because their extra columns span the null spaces of M and Mᵀ, not just the row/column space. The compact SVD drops that padding: for a rank-r matrix, U shrinks to m×r, Σ to r×r diagonal holding only the r nonzero singular values, V to n×r — this still reconstructs M exactly, since rank(M)=r, it just discards the null-space columns that contributed nothing. The truncated (rank-k) SVD used above for the ratings matrix goes further: keep only the top k<r singular vectors — U becomes m×k, Σ becomes k×k, V becomes n×k — which is lossy by design, since only the k dominant latent factors matter for prediction.
The Eckart-Young theorem proves that keeping only the top-$k$ singular vectors gives the best possible rank-$k$ approximation: $M_k = sum_{i=1}^k sigma_i u_i v_i^T$. No other rank-$k$ matrix is closer to $M$ in either Frobenius or spectral norm. This is the mathematical guarantee behind every truncated SVD application — compression, denoising, dimensionality reduction.
Rank itself is a hard quantity to optimise directly: minimising rank(M) subject to matching a set of observed entries is NP-hard, because rank counts how many singular values are nonzero — the same L0-style combinatorial difficulty as minimising the number of nonzero entries in a vector. The standard fix mirrors the vector case: just as the L1 norm (sum of absolute values) is the convex relaxation used to encourage a sparse vector, the nuclear norm ‖M‖* = Σσᵢ (the sum of the singular values) is the convex relaxation used to encourage a low-rank matrix. Minimising the nuclear norm instead of rank is a tractable convex problem, and because it penalises the sum of singular values rather than merely their count, it pushes small singular values toward zero — the matrix version of L1 shrinking small coefficients to zero. This is the trick behind matrix completion algorithms (recovering a full matrix from a few observed entries, e.g. filling in the rest of that ratings matrix).
NOT this. Most people treat SVD and PCA as synonyms. They are not. SVD is a matrix factorization — a numerical decomposition that works on any matrix. PCA is a statistical method for finding directions of maximum variance in a dataset. PCA uses SVD as its computational engine: on mean-centered data, the right singular vectors of the data matrix equal the eigenvectors of the covariance matrix, and the two methods produce the same result. But SVD is more general (works on rectangular matrices, handles non-statistical applications like pseudo-inverses), and numerically more stable than computing the covariance matrix $X^T X$ explicitly, which squares the condition number (condition number κ = σ_max/σ_min, the ratio of the largest to smallest singular value — the higher it is, the more small numerical errors get amplified when you invert or solve with that matrix).
Key points
- Use SVD when you need a low-rank approximation, a pseudo-inverse, or intrinsic dimensionality. Call `np.linalg.svd(X, full_matrices=False)` and inspect the singular value spectrum. A sharp elbow in the scree plot tells you the effective rank of your data. Singular values below the elbow are noise — including them adds variance without signal to any downstream model.
- The production trap: computing PCA via eigendecomposition of the covariance matrix $X^T X$. This squares the condition number: if $κ(X) = 100$, then $κ(X^T X) = 10,000$. Numerical errors get amplified by $κ^2$. Always compute PCA via SVD of the data matrix directly. sklearn's `PCA` does this by default. If you wrote your own PCA from scratch using `np.linalg.eig` on $X^T X$, replace it.
- The diagnostic: plot the singular value spectrum and check the condition number $κ = sigma_{max}/sigma_{min}$. A condition number above $10^6$ means the matrix is nearly singular and any computation involving its inverse (least squares, linear regression) will be numerically unstable. The pseudoinverse $A^+ = V Sigma^+ U^T$ handles this by zeroing near-zero singular values rather than inverting them — sklearn's LinearRegression uses this by default.
SVD reveals intrinsic dimensionality (the singular value spectrum) and numerical stability (the condition number) in a single call — read both before trusting any computation on that matrix.
Recap
- SVD $M \approx U\Sigma V^T$ factors any matrix — rectangular OK, unlike eigendecomposition (square only).
- Latent factors: U = user vectors, V = item vectors, Σ = factor importance; predict via $u_i^T\Sigma v_j$ (Netflix Prize).
- Singular values $\sigma_i=\sqrt{\lambda_i(M^TM)}$; U eigenvectors of MMᵀ, V of MᵀM.
- Eckart-Young: top-k singular vectors give the best rank-k approximation — the guarantee behind truncated SVD.
- SVD ≠ PCA: SVD is a matrix factorisation; PCA is variance-finding that *uses* SVD as its engine.
- Compute PCA via SVD of X, not eig of XᵀX — forming XᵀX squares the condition number ($\kappa^2$).
- Condition number $\kappa=\sigma_{max}/\sigma_{min}$; κ > 1e6 → nearly singular, inverse-based computations unstable.
Check your understanding
Q1. A matrix M has SVD M = UΣVᵀ. Which TWO of the following correctly describe U, Σ, V and their dimensions for an m×n matrix with rank r?
- A) U is m×r (left singular vectors), Σ is r×r diagonal (nonzero singular values only), Vᵀ is r×n (right singular vectors) — this is the compact SVD. The full SVD pads U to m×m and V to n×n with extra columns spanning the null spaces. Mᵣ = UΣVᵀ at these dimensions reconstructs M exactly, since rank(M)=r.
- B) U is n×n (input space), Σ is n×m (scaling), V is m×m (output space). The action of M = UΣVᵀ: U rotates in input space, Σ scales each dimension, Vᵀ is not a rotation because it maps between spaces of different dimensions. The singular values on the diagonal of Σ are the square roots of eigenvalues of MMᵀ.
- C) U is m×r, Σ is r×r diagonal, V is n×r. Singular values σ₁ ≥ ... ≥ σᵣ > 0. The action of M=UΣVᵀ: U projects the r-dimensional input subspace, Σ scales, V maps to the output — the roles of U and V are swapped from the standard convention, so the pseudoinverse becomes M⁺ = UΣ⁻¹Vᵀ instead of VΣ⁻¹Uᵀ.
- D) For M: m×n with rank r: U is m×m orthogonal (eigenvectors of MMᵀ); Σ is m×n diagonal (singular values σ₁≥...≥σᵣ>0, zeros elsewhere); V is n×n orthogonal (eigenvectors of MᵀM). The action: V rotates the input, Σ scales, U rotates the output. Best rank-k approx: Mₖ = Σᵢ₌₁ᵏ σᵢuᵢvᵢᵀ — the Eckart-Young theorem.
Q2. In a recommender system, you have a 10,000-user × 5,000-movie rating matrix M. You compute a truncated SVD with k=50. Explain what the k=50 components represent and how you would predict a missing rating.
- A) Truncated SVD Mₖ = UₖΣₖVₖᵀ: Uₖ is 10,000×50 (each user's latent preference vector); Σₖ is 50×50 diagonal (importance of each factor); Vₖᵀ is 50×5000 (each movie's latent attribute vector). The 50 components are latent factors, uninterpretable but capturing co-rating patterns. To predict M_{ij}: compute uᵢᵀΣₖvⱼ, the dot product of user i's and movie j's latent vectors.
- B) The k=50 components are the 50 most popular movies — the singular values rank movies by total rating activity. Uₖ is a 10,000×50 matrix where each row gives a user's ratings for the top-50 movies. To predict M_{ij}: find the nearest user in the top-50 movie subspace and copy their rating for movie j. The truncated SVD provides both dimensionality reduction and a nearest-neighbor lookup structure.
- C) The k=50 SVD components represent 50 user clusters. Uₖ contains cluster assignments (soft), Σₖ contains cluster sizes, Vₖᵀ contains cluster-to-movie affinity scores. To predict M_{ij}: identify user i's cluster membership from row i of Uₖ, then use the cluster's movie preferences from Vₖᵀ. Missing ratings are predicted by the weighted average of cluster preferences, weighted by cluster membership probability and normalized per user.
- D) The k=50 components represent the 50 highest-variance rating patterns across users and movies. Uₖ contains the top-50 left singular vectors describing user variance, Vₖᵀ contains the top-50 right singular vectors describing movie variance. To predict M_{ij}: interpolate between observed ratings using the low-rank structure — M_{ij} ≈ mean_rating + uᵢᵀvⱼ where the dot product captures user-movie affinity after mean-centering.
Q3. The nuclear norm of a matrix is the sum of its singular values. Why is it used as a convex relaxation for minimising rank?
- A) The nuclear norm is a convex relaxation of rank because it is the dual norm of the spectral norm (largest singular value). Any norm can serve as a regulariser; the nuclear norm specifically penalises the sum of singular values, which encourages the matrix to have a small spectral radius rather than low rank, since spectral radius bounds the nuclear norm from below.
- B) The nuclear norm ‖M‖_* = Σσᵢ equals rank(M) when all singular values are exactly 1 (orthogonal matrices), and is larger otherwise. Minimising the nuclear norm subject to constraints therefore minimises how far the singular values deviate from 1, which indirectly minimises rank by shrinking small singular values toward zero before large ones.
- C) Rank minimisation is NP-hard: minimise rank(M) subject to constraints. The nuclear norm ‖M‖_* = Σσᵢ is the tightest convex relaxation of rank — the analogy to L1/L0: L0-norm is NP-hard to minimise, L1 is its relaxation and gives sparsity. rank(M) counts nonzero singular values; nuclear norm sums them, promoting small singular values just as L1 promotes small absolute values.
- D) The nuclear norm is convex because it is a sum of convex functions (each σᵢ is convex in the matrix entries). It relaxes rank minimisation because rank(M) = lim_{p→0} ‖σ‖_p^p (the L0 norm of singular values), and the nuclear norm is the nearest convex function above this limit. The nuclear norm ball {M : ‖M‖_* ≤ 1} is the convex hull of rank-1 matrices with unit spectral norm, used in matrix completion bounds.
Try it interactively
ML Systems Lab is a free interview-prep platform for ML engineers — work through the full interactive module, quizzes, and drills.
Open ML Systems Lab →