Singular Value Decomposition

Step 7 of 18 in Foundations · Theorem · Medium

Every matrix factors as rotation × scaling × rotation (A = UΣVᵀ). Keeping only the largest singular values gives the best low-rank approximation (Eckart–Young theorem). Behind PCA, compression and recommenders.

The lesson

Any matrix of any shape factors as A = UΣVᵀ. Read right to left: Vᵀ rotates the input, Σ stretches along the axes by the singular values σ₁ ≥ σ₂ ≥ … ≥ 0, and U rotates the result into the output space.

The same thing written as a sum: A = σ₁u₁v₁ᵀ + σ₂u₂v₂ᵀ + … Each term is a simple rank-1 layer, and they come in order of importance. Keep only the first k and you have the best rank-k approximation of A. No other rank-k matrix is closer; that is the Eckart–Young theorem.

Worked example: compressing a 1000 × 1000 image
  1. The full image is 1,000,000 numbers.
  2. At rank 50 you store 50 columns of U, 50 singular values and 50 rows of Vᵀ: 50 × (1000 + 1 + 1000) = 100,050 numbers.
  3. About 10% of the storage. The error is exactly what you dropped: √(σ₅₁² + σ₅₂² + …).
U, S, Vt = np.linalg.svd(A, full_matrices=False)
A_k = U[:, :k] * S[:k] @ Vt[:k]    # rank-k approximation

PCA is the SVD of centred data. Recommenders factor a users × items matrix the same way.

Common mistake. Singular values are not eigenvalues. They are the square roots of the eigenvalues of AᵀA, they are never negative, and they exist for non-square matrices.

Check yourself

  1. A is 200 × 100. You keep rank 10. How many numbers do you store for U, S and Vᵀ together?

    Show the answer

    3010. 10 × (200 + 1 + 100) = 3,010, against 20,000 for the full matrix.

  2. A matrix has singular values 10, 5, 0.1 and 0.01. Which rank keeps nearly all of it?

    • 1
    • 2
    • 4
    Show the answer

    2. The last two layers contribute almost nothing. Rank 2 drops only √(0.1² + 0.01²) ≈ 0.1 of error.

  3. The best rank-k approximation of A keeps:

    • the k largest singular values
    • the k smallest singular values
    • the first k rows of A
    Show the answer

    the k largest singular values. The layers are ordered by how much of A they carry. Keeping the largest k leaves the smallest possible error.

Build it

  1. Compress an image

    Keep the top k singular values of a grayscale photo for k = 5, 20, 50; plot reconstructions and storage saved.

    Done when: You can state the smallest k that looks recognizable and its compression ratio.

Case study: Eigenfaces

  • Faces are flattened into vectors; the top singular vectors look like ghostly 'average faces'.
  • Any face is approximated as a weighted mix of a few dozen eigenfaces.
  • It fails under lighting changes, which motivated later learned features (CNNs).

Read the case study

Resources

Earn this star

On the map, your answers are marked and you log what you built. Finish the step and its star lights.

Open this step on the map