NumPy: Fast Arrays
The array library under all of ML: shapes, indexing, boolean masks, axes, broadcasting and vectorised maths.
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.
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.
Creating, indexing and slicing
Boolean masks select elements that satisfy a condition, and are the NumPy version of a SQL WHERE:
A slice is a view of the same memory: changing X[:, 1] changes X. Use .copy() when you need an independent array.
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:
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: 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.
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.
Linear algebra in NumPy
The @ operator multiplies matrices: if is and is , then A @ B is , with entries . A linear model's predictions for every row at once are , which is X @ w + b.
Least squares solves . The normal equations give , 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.
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.
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.
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?
📚 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.
- NumPy: the absolute basics for beginners ↗ Official NumPy guide