Lesson 10 of 116 min
SVD and low-rank approximation
Every matrix is a turn, a stretch and a turn, so it splits into pieces, strongest first; keep the strongest few and you keep most of the matrix in far fewer numbers.
After this lesson you can compress a matrix by keeping its strongest directions.
Loading the video…
Refreshers
An idea from an earlier lesson shows up here when the video leans on it.
In real code
What the video left out, as you will meet it in your own code.
The three parts give the matrix back
As a formula, SVD is A = U Σ Vᵀ: a turn, a stretch, a turn, with Σ as np.diag(S). Multiply the three back and you get the matrix again. The 0 comes back as -2.5e-17, a rounding error that np.allclose ignores. The last line checks that U and Vt are turns: each, undone by its flip, moves nothing.
import numpy as np
M = np.array([[2, 3], [0, 2]])
U, S, Vt = np.linalg.svd(M)
print(U @ np.diag(S) @ Vt)
print(np.allclose(U @ np.diag(S) @ Vt, M))
print(np.allclose(U.T @ U, np.eye(2)), np.allclose(Vt @ Vt.T, np.eye(2)))[[ 2.00000000e+00 3.00000000e+00] [-2.51898468e-17 2.00000000e+00]] True True True
A matrix that is not square
The photo has 240 rows and 180 columns, so it has only 180 pieces. By default NumPy still gives a U of 240 by 240: sixty of its columns belong to no piece. Real code asks for the short form with full_matrices=False. The photo is the video's cat on Wikimedia Commons, saved as cat.jpg.
from PIL import Image
photo = Image.open("cat.jpg").convert("L").resize((180, 240), Image.LANCZOS)
photo = np.asarray(photo, dtype=float)
U, S, Vt = np.linalg.svd(photo)
print(U.shape, S.shape, Vt.shape)
U, S, Vt = np.linalg.svd(photo, full_matrices=False)
print(U.shape, S.shape, Vt.shape)(240, 240) (180,) (180, 180) (240, 180) (180,) (180, 180)
How close each number of pieces gets
The video measured ten pieces. Here are more: the pieces kept, the numbers they cost, and the gap to the photo as a share of its length. Each piece added helps less than the one before. No other way of storing the photo in that many pieces gets closer: that is a proven fact, the Eckart–Young theorem.
for k in [1, 5, 10, 20, 50]:
cat = U[:, :k] * S[:k] @ Vt[:k]
gap = np.linalg.norm(photo - cat) / np.linalg.norm(photo)
print(k, k * (240 + 180 + 1), round(gap, 3))1 421 0.255 5 2105 0.149 10 4210 0.101 20 8420 0.068 50 21050 0.036
Singular values are not eigenvalues
For a matrix that is the same across its diagonal, like [[2, 1], [1, 2]], the two lists match: 3 and 1. For the video's matrix they do not. Its one own line stretches 2 times, so both its eigenvalues are 2, while its stretches are 4 and 1.
for A in [np.array([[2, 1], [1, 2]]), M]:
print(np.linalg.eigvals(A).real, np.linalg.svd(A, compute_uv=False))[3. 1.] [3. 1.] [2. 2.] [4. 1.]
Run on Python 3.13, NumPy 2.5.5, Pillow 13.0.0 and scikit-learn 1.9.1. Checked on .
Quiz
Two questions on this lesson, and one from earlier. Answer as often as you like.
1 of 3
From earlier: Rank and low-rank matrices
The matrix [[2, 6], [3, 9]] has four different numbers. What is its rank?
2 of 3
A photo of a cat is a grid of numbers. You keep its ten strongest pieces: about a tenth of its numbers. What do you see?
3 of 3
A photo has 240 rows and 180 columns. You keep it with 20 pieces. Each piece is a column of 240 numbers, a row of 180, and its singular value. How many numbers do you store?
The ideas in this lesson
- SVD
- Every matrix is a turn, a stretch and a turn; its stretches, largest first, are its singular values.
- Low-rank approximation
- Keep a matrix's pieces with the largest singular values: the closest matrix that so few pieces can make.
It leans on
- PCA
Transcript
What the video says, part by part. Press a time to watch from there.
The game
Here's a game. This photo is a grid of numbers. Each square of gray is one number. Say you keep only a tenth of those numbers, chosen well. What will you see? Part of the photo? A gray mess? Or the whole cat?
A photo is a matrix
A matrix is a grid of numbers, and so is this photo. Each square is one number. Zero is black, and two hundred and fifty-five is white. Two hundred and forty rows, and a hundred and eighty columns.
Pieces, strongest first
This matrix can be split into pieces, strongest first, and each piece covers the whole photo. Here are the ten strongest, added up. The whole cat... a little blurred.
From a little over four thousand numbers: about a tenth.
If you said part of the photo, or a gray mess, here's why not. Every piece reaches every square, so even a few pieces show all of it.
So what is a piece?
A stretched circle
Every matrix splits into pieces, and a small matrix shows how.
This one has only one eigenvector line: the arrow one, zero, which it stretches two times. Every other arrow turns.
So that one line doesn't tell you much about it. Watch a circle of arrows instead. It lands on a stretched circle, with one longest arrow, and one shortest... at a right angle.
Right angles, before and after
Which arrows of the circle land on those two?
Where did they start?
The arrow one, two lands on eight, four: four times as long.
The arrow minus two, one lands on minus one, two: as long as before. They were at a right angle... and they still are.
A turn, a stretch, a turn
So here's what the matrix does. It turns those two arrows onto the line across and the line up. It stretches four times across, and one time up. And it turns them again, onto eight, four and minus one, two. Every matrix, of any size, is a turn, a stretch and a turn.
That's its singular value decomposition... SVD.
Its stretches, largest first, are its singular values: four and one.
Each stretch is a piece
Each stretch makes one piece: a column times a row, like a times table, times its singular value. The column is the direction it lands on, and the row is the direction it starts from, both shrunk to length one. The strong piece, with four, is roughly the matrix already. The weak piece, with one, fixes the rest.
The photo's pieces
A matrix splits into pieces, strongest first, and so does this photo. It has two hundred and forty rows and a hundred and eighty columns. So it has a hundred and eighty pieces, one for each direction of its shorter side. Their singular values fall fast. The first is over twenty-six thousand: that piece holds the photo's average gray. The second is about thirty-six hundred. And number fifty is under two hundred. The strongest piece alone looks like stripes: a column of grays times a row of grays, like a times table in school.
Keep the strongest
What does one piece cost?
A column of two hundred and forty numbers, a row of a hundred and eighty, and one more number, its singular value. That's four hundred and twenty-one numbers.
A small singular value means a weak piece, so the weak pieces you leave out change the picture very little.
Five pieces: a blur.
Ten: the cat. Twenty... fifty: almost the photo, for half its numbers.
The strongest pieces give the closest picture you can make with that few pieces.
That's a low-rank approximation.
On anything?
So can you store any matrix in fewer numbers this way? Here's one that's nothing but random grays.
Static has no strong pieces
Static: random grays, the same size as the photo. Look at its singular values. What's different?
After the first, they're all about the same. There are no strong pieces.
So twenty pieces keep only its average gray. The static itself is gone.
So this works when a few pieces are strong. Photos are like that, and so is most real data.
In NumPy
In NumPy, one line splits a matrix into its pieces, and gives back three things. The first holds the directions the pieces land on, as columns. The last holds the directions they start from, as rows. The middle one holds the singular values.
For our small matrix, what will the middle one hold?
Four and one.
For ten pieces of the photo, keep the first ten columns, the first ten singular values and the first ten rows. That's the cat, in a little over four thousand numbers. How close is it? Its distance from the photo, divided by the photo's own length, is about a tenth.
Machine learning does this to tables of data too: it keeps their strongest directions. That's called PCA, and the PCA in scikit-learn runs on SVD.
A turn, a stretch, a turn
So every matrix is a turn, a stretch and a turn. It splits into pieces, strongest first. Keep the strongest few, and you keep most of the matrix in far fewer numbers.
Keep the strongest
So a few strong pieces hold most of a matrix. Now try it on this lesson's page, and take the quiz. Find how many pieces cost as many numbers as the photo.
You use this lesson in Lab 4: Nearest digits, in fewer numbers, after High-dimensional space.
Facts last checked on .
