Lesson 5 of 16 · Matrices
What Is a Matrix?
Learn what a matrix is, how to read its shape and entries, how a whole dataset or an image becomes a matrix, and how to select rows and columns and work along axes with NumPy.
- 8 min read
- Intermediate
- Reviewed
- What a matrix is and how to read its shape
- How to name and find a single entry
- Why a dataset and an image are both matrices
- How to select rows, columns and entries in NumPy
- How to summarise each column or row with the axis argument
A grid of numbers
A vector is a list of numbers. A matrix is a whole table of them.
column 1 column 2 column 3
A = [ 2 0 5 ] row 1
[ 1 4 3 ] row 2The order always matters: rows first, then columns. A 2 × 3 matrix and a 3 × 2 matrix are different shapes.
Entries
A vector is just a matrix with one row (a row vector, shape 1 × n) or one column (a column vector, shape n × 1).
Matrices in NumPy
A NumPy matrix is a list of rows:
import numpy as np
A = np.array([
[2, 0, 5],
[1, 4, 3],
])
print(A)
print("Shape:", A.shape) # (rows, columns)
print("Entry in row 1, column 3:", A[0, 2]) # NumPy counts from 0
print("Number of entries:", A.size)Remember the shift: maths says a₁₃, NumPy says A[0, 2].
A dataset is a matrix
This is why matrices are everywhere in machine learning:
import numpy as np
# Each row is a house: [size m², bedrooms, age years]
X = np.array([
[120, 3, 15],
[ 80, 2, 30],
[200, 5, 2],
[ 95, 2, 10],
])
print("Shape:", X.shape, "-> 4 houses, 3 features")
print("Second house:", X[1]) # a row: one example
print("All sizes:", X[:, 0]) # a column: one feature
print("Age of the third house:", X[2, 2])The slice X[:, 0] reads as "every row, column 0". The colon on its own means "all of them".
Working along an axis
The rule that makes it easy to remember: the axis you name is the one that disappears.
import numpy as np
X = np.array([
[120, 3, 15],
[ 80, 2, 30],
[200, 5, 2],
[ 95, 2, 10],
])
print("Mean of each feature:", X.mean(axis=0)) # collapse the rows: one value per column
print("Sum of each house's row:", X.sum(axis=1)) # collapse the columns: one value per row
print("Largest size:", X[:, 0].max())X.mean(axis=0) averages down the rows, giving one mean per feature. This is the first step of almost every data analysis.
An image is a matrix
A greyscale image is a matrix of brightness values, one per pixel. A colour image is three matrices, one each for red, green and blue.
import numpy as np
# A tiny 5 x 5 image of a plus sign: 0 = black, 9 = white
image = np.array([
[0, 0, 9, 0, 0],
[0, 0, 9, 0, 0],
[9, 9, 9, 9, 9],
[0, 0, 9, 0, 0],
[0, 0, 9, 0, 0],
])
print("Shape:", image.shape)
print("Brightest value:", image.max())
for row in image: # draw it with characters
line = ""
for pixel in row:
if pixel > 0:
line += "#"
else:
line += "."
print(line)Common mistakes
Summarise a small dataset stored as a matrix.
import numpy as np
# Test scores of 4 students (rows) in 3 subjects (columns: maths, science, english)
scores = np.array([
[80, 72, 90],
[65, 88, 70],
[92, 95, 85],
[70, 60, 75],
])
# 1. Print the shape.
# 2. Print the average score in each subject (one per column).
# 3. Print each student's total score (one per row).
# 4. Print the science score of the third student.
# Output must be exactly:
# Shape: (4, 3)
# Subject averages: [76.75 78.75 80. ]
# Student totals: [242 223 272 205]
# Third student's science: 95
A dataset matrix X has shape (500, 8). What does X.mean(axis=0) give?
Summary
- A matrix is a grid of numbers. Its shape is rows × columns.
- The entry aᵢⱼ is in row i, column j. In NumPy it is
A[i - 1, j - 1]. - A data matrix X has one row per example and one column per feature. Images are matrices too.
X[i]selects a row andX[:, j]a column.- The axis argument chooses the direction:
axis=0gives one result per column,axis=1one per row.
Next: matrix multiplication, the operation that powers neural networks.
Finished reading? Tick it off to track your progress.