Machine learning

Stage 1 · Lesson 4 of 9

Vectorisation and broadcasting

Python for data

means working on a whole array in one go, such as prices * quantities, instead of looping over items one by one in Python. The looping still happens, but inside fast compiled C code, which is often 50 to 100 times faster. Broadcasting is the rule that lets arrays of different shapes combine.

lines the shapes up from the right, and each pair of sizes must be equal or one of them must be 1. So you can subtract a (4,) array of column means from a (1000, 4) array, and every row gets centred. You will carry this way of thinking into pandas and .

Common trapbroadcasting that works when you did not mean it to: (1000,) minus (1000, 1) quietly gives a (1000, 1000) array instead of an error.

A question to check yourself, an AI practice session and 1 cheat sheet entry come with the full roadmap. Unlock

Resources for this stage

Start with· follow one of these

Practise

  • The Python, Pandas and Data Visualization micro courses, in browser notebooks

Keep handy

  • Start with 10 minutes to pandas, then look up indexing, groupby and reshaping

  • Quick NumPy, pandas and Matplotlib lookups with a try-it editor

Mark this stage as not done?