Module 8: SVD
SVD isn't just for decomposing matrices — it lets you throw away the unimportant parts and keep only the structure that matters.
Any matrix can be written as a sum of rank-1 matrices:
Each term σiuiviT is a rank-1 matrix — an outer product scaled by the singular value. The singular values are sorted largest-first, so the first few terms capture the most "energy" in the matrix.
Keep only the first k terms and you get the rank-k approximation:
By the Eckart–Young theorem, Aₖ is the closest rank-k matrix to A (in Frobenius norm). There's no better way to compress a matrix to rank k.
An m×n matrix has mn values. A rank-k approximation stores k copies of (u vector, v vector, σ scalar) = k(m + n + 1) values. For large matrices with small k, the savings are dramatic.
The interactive shows an 8×8 grayscale matrix treated as an image. The slider adds more rank-1 pieces. At rank 1 you see the roughest structure; by full rank it's exact. Watch how quickly the approximation improves with each additional piece.
import numpy as np
# img is an m×n grayscale matrix
U, s, Vh = np.linalg.svd(img, full_matrices=False)
def compress(k):
return U[:, :k] @ np.diag(s[:k]) @ Vh[:k, :]
# k=10 often retains most of the visual quality
approx = compress(10)
compression_ratio = (img.size) / (k * (U.shape[0] + Vh.shape[1] + 1))Original storage: 64 values
Rank-1 storage: 17 values (1 × (u + v + σ))
73% smaller · · with some loss
Each rank-1 piece is one singular value + two vectors. Drag the slider to add more pieces.