Why arrays
Last updated
A machine learning model never looks at one number at a time. It takes a whole table of numbers, thousands of rows of them, and does the same sums to every row. Python lists are too slow for that, so every tool in this course uses NumPy arrays instead. In this lesson you'll make your first array, see how it differs from a list, and time the two against each other.
By the end of this lesson you can
- Make a NumPy array from a list and do maths on all of it at once
- Read an array's
dtypeandshape - Explain why a loop over a list is slower than
np.sum - Say why
*means something different on a list and on an array
A list against an array
In Simple Python you kept numbers in a list. NumPy keeps them in an array. Here are five prices in pesos, first as a list, then as an array. Run the cells and compare.
import numpy as np loads NumPy under the short name np. Everyone writes it that way, so you'll see np. in every example you find.
The second cell is the point: arr * 1.12 adds 12% VAT to every price at once. With a list you need a loop, or a list comprehension like the third cell, to visit each price yourself.
The answers are the same numbers, shown differently. 50.400000000000006 is how a computer stores 45 × 1.12, because most decimals can't be stored exactly in binary. NumPy rounds what it shows to 8 digits, so the array looks tidier, but the numbers inside are the same.
A model does this all the time: multiply every input by a weight, add every bias, apply a function to every value. Arrays let you write each of those as one line.
One type, packed together
Why can an array do maths on everything at once? Because of how it keeps the numbers. Step through:
A list holds pointers to separate Python objects, which can be anything: a number, a string, another list. An array holds the numbers themselves, side by side, and all of the same type. That's what lets NumPy's code, written in C, run straight along them.
The race
Here's the difference in speed, first as a picture:
Now for real. The notebook makes 50,000 numbers as a list and as an array, checks that a loop and np.sum agree (True), then times each one. %timeit is a notebook command: it runs a line many times and reports how long one run takes on average. Run all four cells.
On most computers np.sum is 30 to 100 times faster. Your numbers won't match anyone else's, because they depend on the machine, but the gap is always large.
It matters more in machine learning than almost anywhere else. Training a model means running the same sums over the whole dataset hundreds or thousands of times. A gap of 50 times is the gap between a model that trains while you make coffee and one that trains overnight.
dtype and shape
Every array has a dtype (the one type all its numbers share) and a shape (how many numbers there are along each direction). Run these:
flippersholds whole numbers, so its dtype is an integer type. Its shape(5,)means one direction, 5 long. The comma is there because a shape is always a tuple, even with one number in it.masseshas one decimal in it,3800.5, so the whole array becomesfloat64: decimals, 8 bytes each. One type for all, so the whole numbers became decimals too.- Mixing a string in turns everything into text (
<U11means Unicode strings up to 11 characters). You can't do maths on that. When an array of numbers comes out as strings, something in the data wasn't a number. penguinsis a list of lists, so the array has two directions: 3 rows (penguins) by 2 columns (flipper length, mass). Its shape is(3, 2),ndim(number of dimensions) is 2, andsize(how many numbers in all) is 6.
That last one is how all data looks in this course: one row per example, one column per measurement. Lesson 0.2 builds arrays like it.
On your own computer, whole numbers show as int64 (8 bytes each). This page says int32, because the Python in your browser is a 32-bit one. Nothing in this stage depends on the difference.
Wrong way: doubling a list
The same symbol can mean different things for a list and for an array. Run both cells.
For a list, * 2 means "repeat the list twice": [1, 2, 1, 2]. For an array, it means "multiply every number by 2": [2, 4]. Neither is an error, so Python won't warn you if you pass a list where you meant an array.
This is the first of many mistakes in this course that don't crash. A model fed a list repeated twice will still train; it will just learn from the wrong numbers. When a result has twice as many items as you expected, check what you had with type().
Check what you learned
Sources
Every example on this page was run with Python 3.14, and its output and error messages are copied from it. These pages cover the same ground in more depth.
- NumPy: What is NumPy?: arrays against lists, and why vectorised code is faster
- NumPy: the absolute basics for beginners: making arrays, import numpy as np, shape and ndim
- NumPy: Data types: every element of an array has the same dtype
- The Python Tutorial: Lists: what * does to a list
- Harris et al., Array programming with NumPy, Nature 585, 2020: how the array works, by the people who make NumPy