Easier to read with cream paper, a tint, more contrast or larger text? Choose your colours and text, once, for every page.

Free lesson

How to multiply matrices

After this lesson: I can multiply a matrix by a vector and by another matrix.

60 min lesson3 commandsJupyterLab data science lab: pandas, Matplotlib and scikit-learn in the browser

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

WordWhat it meansExample
Matrixa grid of numbers in rows and columns
Rowone line of a matrix, read across
Columnone line of a matrix, read down
Shapethe number of rows, then the number of columns2 x 3
Matrix productthe 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

DayColumn 1Column 2: degreesColumn 3: rain in mm
Day A1100
Day B1205
Day C1010

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

DayWorking with the weights 140, 11 and -7Predicted hires
Day A1 x 140 + 10 x 11 + 0 x -7 = 140 + 110 + 0250
Day B1 x 140 + 20 x 11 + 5 x -7 = 140 + 220 - 35325
Day C1 x 140 + 0 x 11 + 10 x -7 = 140 + 0 - 7070

The cells students fill in are left blank in this preview.

Predict three days in one product

Command
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

Code: python
# 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 answerRow of the firstColumn of the secondWorkingValue
Row 1, column 1[1, 2][0, 1]1 x 0 + 2 x 12
Row 1, column 2[1, 2][1, 0]1 x 1 + 2 x 01
Row 2, column 1[3, 4][0, 1]3 x 0 + 4 x 14
Row 2, column 2[3, 4][1, 0]3 x 1 + 4 x 03

The cells students fill in are left blank in this preview.

Multiply both ways round

Command
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

Command
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.

About this lab and its source: JupyterLab data science lab: pandas, Matplotlib and scikit-learn in the browser

Teaching a class? Set this module for your students with a teacher guide, marked assignments and a lab for every student.