Python, from Zero, for AI · Chapter 08
A row of pointers and a block of numbers
1:43 · Narrated with a synthetic voice. Captions are available in the player.
Read the transcript
Module eight is numbers in bulk, and it explains the speed difference rather than asserting it. A Python list of a million numbers is a row of pointers, each leading to a separate object with a type tag and a reference count, so every operation pays a lookup and an allocation per element. A NumPy array is one block of memory holding one type, so the same operation is a compiled loop over raw bytes. That is a hundredfold difference, and it is why the answer to slow numeric Python is never a better written loop. You move the loop out of Python. That fact is what makes search over your own documents possible on an ordinary machine. An embedding is a row of floating point numbers. Normalise every row of the matrix once, and the similarity of a question to a hundred thousand documents becomes a single matrix multiplication taking a few milliseconds. The module also covers shapes and the broadcasting bug that runs without complaining, the difference between a view and a copy, pandas for tables, the join that silently multiplies your rows, and cleaning the values that are missing without being empty. A payment file for four thousand farmers grew by forty-four rows for exactly that reason.