Vectorised computing

Element-wise arithmetic and ufuncs, broadcasting rules for arrays of different shapes, and reductions along axes.

About 5 hours of study

Learning objectives

Lessons

  1. 1

    Vectorised arithmetic and ufuncs Intermediate

    Arithmetic operators that work element by element, operations with a single number, universal functions such as sqrt and exp, division by zero producing inf and nan, and why vectorised code replaces loops.

  2. 2

    Broadcasting Advanced

    How NumPy combines arrays of different shapes: lining shapes up from the right, stretching dimensions of size 1, the error when shapes do not fit, and common uses such as centring columns and building tables.

  3. 3

    Reductions along axes Intermediate

    Summarising arrays with sum, mean, min, max, std, argmin and argmax, choosing an axis to reduce along, keeping dimensions with keepdims, and running totals with cumsum.

Module practice

Module quiz

1. What is np.array([1, 2, 3]) * 2?
2. Shapes (3, 1) and (1, 4) broadcast to…
3. For m of shape (2, 5), m.sum(axis=1) has shape…
4. Do shapes (3,) and (4,) broadcast?
5. What does np.array([1.0, 0.0]) / 0 produce?
6. Why use keepdims=True when dividing rows by their sums?

‹ Indexing and memoryShaping, ordering and statistics ›