Lesson 5 of 117 min
A matrix is a transformation
A matrix moves every point of the plane at once, and its columns say where the arrows one step across and one step up land.
After this lesson you can predict what a small matrix does to a shape.
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.
Where a point lands, as a rule
A point [x, y] lands on x times the first column plus y times the second. That is the walk from the video, written out. The @ sign does the same in one step; the next chapter shows how it works row by row.
import numpy as np
M = np.array([[2, 0], [0, 1]])
p = np.array([1, 2])
print(p[0] * M[:, 0] + p[1] * M[:, 1])
print(M @ p)[2 2] [2 2]
A turn by any angle
The video turned by a quarter, where the numbers are 0, 1 and −1. A smaller turn needs np.cos and np.sin, but the reading is the same: the first column is where the arrow across lands, here 30 degrees up from where it was.
a = np.radians(30)
turn = np.array([[np.cos(a), -np.sin(a)],
[np.sin(a), np.cos(a)]])
print(turn.round(3))
print(turn[:, 0].round(3))[[ 0.866 -0.5 ] [ 0.5 0.866]] [0.866 0.5 ]
A slide is an addition
A matrix always keeps the start where it is, so no matrix can slide a picture. To slide, add the same vector to every point. A neural network's layer does both, a matrix and then an addition, which is the next chapter's question.
M = np.array([[0, -1], [1, 0]])
print(M @ np.array([0, 0]))
slide = np.array([[1.5], [1]])
ink = np.array([[-2, -1, 0], [4, 4, 4]])
print(ink + slide)[0 0] [[-0.5 0.5 1.5] [ 5. 5. 5. ]]
A new lean for each copy
The video leaned the zero once. To make many practice pictures, pick a new small lean for each copy. Here each copy prints its lean and where the zero's first point, [-2, 4], lands: the higher a point, the farther it slides.
from sklearn import datasets
img = datasets.load_digits().images[0]
down, across = np.nonzero(img)
ink = np.array([across - 4, 4 - down])
rng = np.random.default_rng(0)
for copy in range(3):
k = rng.uniform(-0.3, 0.3)
practice = np.array([[1, k], [0, 1]]) @ ink
print(round(k, 2), practice[:, 0].round(2))0.08 [-1.67 4. ] -0.14 [-2.55 4. ] -0.28 [-3.1 4. ]
The same in PyTorch
Real training code rarely writes the matrix by hand. torchvision's RandomAffine picks a small turn, stretch and lean for each copy, about the picture's middle, and slides only if you give it translate. It works on the picture's squares, not on points. Its page lists every setting.
import torch
from torchvision.transforms import v2
x = torch.tensor(img, dtype=torch.float32).unsqueeze(0)
aug = v2.RandomAffine(degrees=10, scale=(0.9, 1.1), shear=10)
copies = [aug(x) for _ in range(5)]
print(len(copies), copies[0].shape)5 torch.Size([1, 8, 8])
Run on Python 3.13, NumPy 2.5.5, scikit-learn 1.9.1, PyTorch 2.15.0 and torchvision 0.29.2. Checked on .
Quiz
Two questions on this lesson, and one from earlier. Answer as often as you like.
1 of 3
From earlier: Adding and scaling vectors
Red is [1, 0, 0] and green is [0, 1, 0]. You take 1 × red plus some amount of green, and you get orange, [1, 0.5, 0]. What amount of green?
2 of 3
NumPy prints M = np.array([[1, 1], [0, 1]]) as two rows: [1 1], then [0 1]. What does M do to the handwritten zero?
3 of 3
A move sends the arrow across, [1, 0], to [2, 1], and the arrow up, [0, 1], to [-1, 1]. Where does the point [1, 2] land?
The ideas in this lesson
- Matrix
- A grid of numbers that moves every point at once. Lines stay straight and even, and the start stays put.
- A matrix's columns
- Its first column is where the arrow `[1, 0]` lands, its second where `[0, 1]` lands, and every point follows.
It leans on
Transcript
What the video says, part by part. Press a time to watch from there.
The puzzle
Here's a puzzle. Four numbers in a square can move every point of a picture. These four move this handwritten zero. Look at the minus sign. Will the zero turn on its side? Flip over, like in a mirror? Or lean?
Every point moves
Hold on to your guess.
Four numbers in a square like this are called a matrix. Let's start with a plainer one.
This matrix makes the zero twice as wide. Every point of the plane moves to a new place, all at once. A point's first number, how far across it is, doubles. Its second number, how far up it is, stays the same. And the grid under the zero stretches with it.
What a matrix keeps
So what did the move keep? Look at the grid. Its lines are still straight. They're still parallel, and evenly spaced. And the start, where every arrow begins, stays where it is. So a matrix can stretch a picture, turn it, lean it or flip it. It can't bend it... and it can't slide it. A move like this has a name... a transformation. And a matrix is one.
A point is a walk
A matrix moves every point of the plane. So how can its four numbers say where every point goes?
Here are two arrows. The arrow across is one, zero. The arrow up is zero, one.
Now look at the point at one, two.
It's a walk along the grid's lines.
One step of the arrow across... then two steps of the arrow up.
Every point is a mix of these two arrows.
The walk moves with the grid
Now make the zero twice as wide again. Where does the point at one, two land?
The walk moves with the grid. The arrow across now lands on two, zero. The arrow up stays where it was. So it's one step of the new arrow across... and two steps of the arrow up. Two, two.
So you only need to follow two arrows to know where every point goes.
The columns
The matrix writes those two landing places down... as its columns. The first column is where the arrow across lands. The second column is where the arrow up lands. Two, zero, and zero, one. Four numbers, two arrows. In NumPy, this line pulls out the first column: where the arrow across lands.
The turn
Now, back to the puzzle. Read its columns. The first one is zero, one: the arrow across lands pointing up. The second is minus one, zero: the arrow up lands pointing left.
So the zero... turns on its side.
If you said it would flip: the minus does send an arrow the other way. It sends the arrow up to the left, not the right. But look at both arrows. Each one turned a quarter to the left. That's a turn, not a flip.
Rows or columns?
One more thing. NumPy prints a matrix row by row. So why not read the rows?
The rows mislead
Here's a matrix with one, one on top, and zero, one under it. If you read its rows, the arrow across would land on one, one... and the zero would lean up. Can you spot the mistake?
Read the columns. The arrow across stays where it is.
One, one is there too, but it's where the arrow up lands. So the top slides right... and the zero leans right.
Three directions
And when a point has three numbers?
Then a matrix moves space. A three by three matrix has three columns: where the arrows across, back and up land. Here, the first two columns are the arrows across and back, as they were, so the floor stays where it is. The third column is one, zero, one: the arrow up lands leaning across. So the box leans, as the zero did.
A bigger matrix works the same way: one column for each direction.
Practice pictures
AI uses moves like these. When people train a model to read handwriting, they often make more practice pictures from the ones they have. Each one is turned a little, stretched a little, or leaned a little. Every one of those moves is a matrix, with the middle of the picture as the start.
In NumPy: a practice picture
Let's make a practice picture in NumPy. These lines load the handwritten zero and find its squares with ink. Each square becomes a point: how far across, and how far up, from the middle of the picture. That's thirty-five points, in one array. Here's a small lean, typed row by row, the way NumPy prints it. Will the top of the zero slide right... or will its right side rise?
Read the columns, not the rows. The arrow across stays where it is. The arrow up now lands a little to the right. So the top slides right, and the zero leans.
And the at sign moves all thirty-five points at once. Pick a new small matrix for each copy... and one handwritten zero becomes as many practice pictures as you need.
What you saw
So here's what you saw. A matrix moves every point of the plane at once. Its lines stay straight and evenly spaced, and the start stays put.
Its first column is where the arrow across lands. Its second is where the arrow up lands.
Read the columns, not the rows, and every point follows.
Its columns
So a matrix moves every point at once. Its columns tell you where the arrows land. Now try it on this lesson's page, and take the quiz. Turn the zero a quarter turn to the right.
You use this lesson in Lab 2: A small layer, and a cheap change to it, after Rank and low-rank matrices.
Facts last checked on .
