P02.2 · Broadcasting and vectorization
P02.2 L1 · Core 4 lessons · about 100 min Live · in review
Part of P02 Scientific Python: NumPy, pandas and Plotting in the Programming and Data track.
NumPy’s speed and its brevity come from one habit: write the operation once for the whole array and let the loop run inside the library. These four lessons replace loops with whole-array expressions, collapse a table of temperatures along one axis at a time, state the broadcasting rule that lets arrays of different shapes meet, and put it to work centring and scaling features and building a table of distances, the everyday preparation of data for machine learning.
What you need first. NumPy arrays, their shape, axes and data types (P02.1), and loops, functions and lists in plain Python (P01.3, P01.4, P01.6). The regression points and the line y = 1.4x + 0.5 of ML01 appear as data only; no machine learning is assumed.
Lessons
Vectorisation: the loop moves inside the array P02.2.1 25 min
One whole-array expression replaces a Python loop. Compute predictions, residuals and the mean squared error of a line on four points, first with a loop and then in one line each, and see why NumPy’s version is so much faster.
Reductions along an axis P02.2.2 25 min
sum, mean, min, max and argmax collapse a table along one axis: the axis you name is the one that disappears. Compute the station and day means of four days of temperatures by loops and by NumPy, and keep the collapsed axis with keepdims.
The broadcasting rule P02.2.3 25 min
Arrays of different shapes can meet in one expression when their shapes line up from the right. Learn the rule, see a scalar, a row and a column stretched onto a table, write broadcast_shape in plain Python and check it against NumPy.
Broadcasting at work: centring and scaling P02.2.4 25 min
Centre each station’s temperatures on its mean and scale them to standard deviation 1 with two broadcasts, fix the day means with keepdims, and build a table of distances from a column minus a row: the feature scaling and distances of k-nearest neighbours.
The interactive
P02.2-X1 · Broadcast stretcher. Pick the shapes of two small arrays from six presets and step through the broadcasting rule: the shapes line up from the right, each axis of size 1 is stretched with ghost copies, and the read-out gives the result shape and values, or the axis that fails with NumPy’s error. It lives in The broadcasting rule.