Python
Topic 18 · Python for data · ⏱ about 6 h

NumPy: Fast Arrays

The array library under all of ML: shapes, indexing, boolean masks, axes, broadcasting and vectorised maths.

Show:
In this topic
Beginner

Why NumPy

Python lists are flexible but slow for numbers: a loop over a million values runs one value at a time in the Python interpreter. NumPy stores numbers in a compact array and runs operations in fast compiled code, typically 10–100 times faster. Every ML library (pandas, scikit-learn, PyTorch) is built on the same idea, so the habits you learn here carry over directly.

import numpy as np ages = np.array([23, 67, 45, 81, 34]) print(ages * 2) # [ 46 134 90 162 68] (no loop!) print(ages.mean()) # 50.0 print(ages > 40) # [False True True True False]

An array has a shape (its size along each dimension) and a dtype (the type of every element). A table of 200 patients × 3 measurements is a 2-D array with shape (200, 3): rows first, then columns. Images are 3-D (height, width, colour); a batch of images is 4-D.

Beginner

Creating, indexing and slicing

np.zeros((2, 3)) # 2×3 of zeros np.arange(0, 10, 2) # [0 2 4 6 8] np.linspace(0, 1, 5) # 5 evenly spaced values from 0 to 1 rng = np.random.default_rng(0) # a seeded random generator (reproducible) X = rng.normal(size=(4, 3)) # 4×3 of normal random numbers X[0] # first row X[:, 1] # second column (all rows) X[1:3, :2] # rows 1–2, columns 0–1 X.T # transpose: shape (3, 4) X.reshape(2, 6) # same 12 numbers, new shape

Boolean masks select elements that satisfy a condition, and are the NumPy version of a SQL WHERE:

ages = np.array([23, 67, 45, 81, 34]) old = ages > 60 # [False True False True False] print(ages[old]) # [67 81] print(old.sum()) # 2 (True counts as 1) print(ages[(ages > 30) & (ages < 70)]) # use & and |, with brackets

A slice is a view of the same memory: changing X[:, 1] changes X. Use .copy() when you need an independent array.

Beginner

Axes and broadcasting

Aggregations such as sum, mean and max can work over the whole array or along one axis. axis=0 collapses the rows and gives one result per column; axis=1 collapses the columns and gives one result per row.

Broadcasting lets arrays of different shapes work together: NumPy stretches the smaller one to match, without copying data. Subtracting a row of column means from a whole table is one line:

X = np.array([[1., 200.], [2., 400.], [3., 600.]]) # shape (3, 2) means = X.mean(axis=0) # [ 2. 400.] shape (2,) Z = (X - means) / X.std(axis=0) # (3,2) - (2,) → broadcast across rows print(Z.mean(axis=0)) # [0. 0.]

The rule: compare shapes from the right; dimensions are compatible when they are equal or one of them is 1. A shape (3, 1) column and a (1, 4) row broadcast to (3, 4). Most "operands could not be broadcast" errors are fixed by adding a dimension: v[:, None].

Try it

Try it: axis and broadcasting

Choose an operation and an axis and see which cells are combined. Then switch to broadcasting to see how a row or a column is stretched across a table.

Interactive: this part runs in your browser.

Try it

Code example: NumPy without loops

A worked example: read it step by step (every part is explained), then run it and experiment.

Interactive: this part runs in your browser.

Intermediate · Maths

Linear algebra in NumPy

The @ operator multiplies matrices: if AA is n×kn\times k and BB is k×mk\times m, then A @ B is n×mn\times m, with entries (AB)ij=∑lAilBlj(AB)_{ij} = \sum_{l} A_{il}B_{lj}. A linear model's predictions for every row at once are y^=Xw+b\hat{\mathbf y} = X\mathbf w + b, which is X @ w + b.

Least squares solves min⁡w∥Xw−y∥2\min_{\mathbf w}\lVert X\mathbf w - \mathbf y\rVert^2. The normal equations give w=(X⊤X)−1X⊤y\mathbf w = (X^{\top}X)^{-1}X^{\top}\mathbf y, but forming the inverse is numerically fragile; np.linalg.lstsq(X, y, rcond=None) solves the same problem stably. Other tools you will use: np.linalg.norm (vector length), np.linalg.eigh (eigenvectors of a symmetric matrix, used by PCA) and np.linalg.svd.

Try it

Code example: linear regression with matrices

A worked example: read it step by step (every part is explained), then run it and experiment.

Interactive: this part runs in your browser.

Try it

Cheat sheet: NumPy

The commands you will use most, on one screen. Come back to it while you work.

Interactive: this part runs in your browser.

Check yourself

Quiz

1. X has shape (50, 4). What is X[:, 0].shape?

2. What does X.sum(axis=1) return for a (50, 4) array?

3. Shapes (3, 1) and (1, 4) broadcast to…

4. Which selects values greater than 5 from array a?

5. Why prefer vectorised NumPy over Python loops?

Go deeper

📚 Further reading

Trusted, mostly free sources: official documentation, well-known textbooks and courses. When you want to confirm or go deeper on anything here, start with these.

Ready