What is a matrix?
A matrix is a grid of numbers in rows and columns. Multiplying a matrix by a vector turns one list of numbers into another, which is how a model works out many predictions at once.
This lesson shows how to multiply a matrix by a vector, and by another matrix.
By the end of this lesson
Note
I can multiply a matrix by a vector and by another matrix.
Key words
| Word | What it means | Example |
|---|---|---|
| Matrix | a grid of numbers in rows and columns | |
| Row | one line of a matrix, read across | |
| Column | one line of a matrix, read down | |
| Shape | the number of rows, then the number of columns | 2 x 3 |
| Matrix product | the matrix you get by multiplying two matrices |
The cells students fill in are left blank in this preview.
A matrix has rows, columns and a shape
A matrix has rows, which run across, and columns, which run down. Its shape is the number of rows, then the number of columns. A matrix with 2 rows and 3 columns has shape 2 x 3.
In the table below each row is one day, and the columns are a 1, the temperature and the rain. The matrix has shape 3 x 3.
Worked example: three days as a matrix
| Day | Column 1 | Column 2: degrees | Column 3: rain in mm |
|---|---|---|---|
| Day A | 1 | 10 | 0 |
| Day B | 1 | 20 | 5 |
| Day C | 1 | 0 | 10 |
The cells students fill in are left blank in this preview.
Matrix times vector: a dot product per row
To multiply a matrix by a vector, take the dot product of each row with the vector. Each row gives one number, so the answer has one component per row.
The vector must have as many components as the matrix has columns.
Worked example: predicting hires for three days
| Day | Working with the weights 140, 11 and -7 | Predicted hires |
|---|---|---|
| Day A | 1 x 140 + 10 x 11 + 0 x -7 = 140 + 110 + 0 | 250 |
| Day B | 1 x 140 + 20 x 11 + 5 x -7 = 140 + 220 - 35 | 325 |
| Day C | 1 x 140 + 0 x 11 + 10 x -7 = 140 + 0 - 70 | 70 |
The cells students fill in are left blank in this preview.
Predict three days in one product
docker exec jupyter python3 -c "import numpy as np; x = np.array([[1, 10, 0], [1, 20, 5], [1, 0, 10]]); w = np.array([140, 11, -7]); print((x @ w).tolist())"Shown here, not run. Create a free account to press Run and type it into your own lab’s terminal.
One product gives every day at once
The weights are a simple rule: 140 hires to start with, 11 more for each degree and 7 fewer for each millimetre of rain. The matrix holds the three days, one per row.
The Run printed [250, 325, 70], the same three answers as the table.
Multiply a matrix by a vector
# A matrix times a vector: one dot product for each row.
def row_times_vector(m, v):
answer = []
for row in m:
total = 0
for i in range(len(v)):
total = total + row[i] * v[i]
answer.append(total)
return answer
days = [[1, 10, 0], [1, 20, 5], [1, 0, 10]]
weights = [140, 11, -7]
print(row_times_vector(days, weights))
# Add a fourth day, [1, 15, 2], to days and run it again.
Matrix times matrix: rows dotted with columns
To multiply two matrices, take the dot product of each row of the first with each column of the second. The number in row 1, column 2 of the answer uses row 1 of the first matrix and column 2 of the second.
The shapes must fit. A 2 x 3 matrix times a 3 x 4 matrix gives a 2 x 4 matrix.
The two inner numbers, 3 and 3, must match. Then they drop out of the answer.
Worked example: multiplying two 2 x 2 matrices
| Cell of the answer | Row of the first | Column of the second | Working | Value |
|---|---|---|---|---|
| Row 1, column 1 | [1, 2] | [0, 1] | 1 x 0 + 2 x 1 | 2 |
| Row 1, column 2 | [1, 2] | [1, 0] | 1 x 1 + 2 x 0 | 1 |
| Row 2, column 1 | [3, 4] | [0, 1] | 3 x 0 + 4 x 1 | 4 |
| Row 2, column 2 | [3, 4] | [1, 0] | 3 x 1 + 4 x 0 | 3 |
The cells students fill in are left blank in this preview.
Multiply both ways round
docker exec jupyter python3 -c "import numpy as np; a = np.array([[1, 2], [3, 4]]); b = np.array([[0, 1], [1, 0]]); print((a @ b).tolist(), (b @ a).tolist())"Shown here, not run. Create a free account to press Run and type it into your own lab’s terminal.
The order of a matrix product matters
The Run printed [[2, 1], [4, 3]] [[3, 4], [1, 2]]. The first is A times B, and the second is B times A. They are different.
Here B swaps the two columns of A when it is on the right, and swaps the two rows when it is on the left.
A common mistake
Warning
Many people think a matrix product multiplies each cell by the cell in the same place. That is a different operation. The matrix product takes a row of the first and a column of the second, multiplies matching numbers and adds them.
Check that the shapes fit
docker exec jupyter python3 -c "import numpy as np; print(np.zeros((2, 3)).shape, np.zeros((3, 4)).shape, (np.zeros((2, 3)) @ np.zeros((3, 4))).shape)"Shown here, not run. Create a free account to press Run and type it into your own lab’s terminal.
Going further
In your own notebook, build a matrix of five days from hires.csv with a column of ones, the temperature and the rain. Multiply it by the weights 140, 11 and -7.
Compare the five predictions with the real hires. Say which day the rule gets most wrong.