Thinking in Arrays
“Dwell on the beauty of life. Watch the stars, and see yourself running with them.”
– Marcus Aurelius
With vectors and matrices under our belts, we now generalise to tensors—the containers in which essentially all machine-learning data lives. On the mathematical side a tensor is just a higher-order matrix. On the programming side it is a NumPy ndarray, and almost everything you will write for the rest of the semester is an operation on one.
The goal of this module is fluency, not vocabulary. You are allowed the documentation during labs and exams, so nobody is asking you to memorise that the ceiling function is spelled np.ceil. What you cannot look up in the moment is whether you meant axis=0 or axis=1, whether the thing you just modified was a view or a copy, and why (5, 3) and (5,) refuse to broadcast. Those four ideas—shapes, axes, broadcasting, views versus copies—are the whole module, and between them they explain the overwhelming majority of NumPy tracebacks you will ever read.
By the end of this module you should be able to:
Describe a dataset as a tensor and state its order and shape.
Create arrays, and reason about
dtype,shape, andreshapebefore running anything.Index and slice along any axis, and predict whether the result aliases the original array.
Apply the broadcasting rule by hand to decide whether two shapes are compatible—and what the result shape is.
Use universal functions and aggregations, choosing the correct
axisand knowing when you needkeepdims=True.Read a NumPy error message and locate the offending pair of dimensions.
Before you run a cell, say out loud (or write down) the shape you expect. Then run it. Being wrong and understanding why is what builds the mental model; being right by accident builds nothing.
The dangerous NumPy bugs are not the ones that raise an error. They are the ones that broadcast successfully and hand you a plausible number that happens to be wrong.