AI Hints
Home›Machine Learning Tutorial›NumPy, pandas, and Charts
Machine Learning · Lesson 7 of 85

NumPy in Ten Minutes

Fast maths on whole arrays.

⏱ 2 min read▶ Video at 14:56🎓 Free
Watch this lesson in the Machine Learning courseWatch on YouTube ↗Full Machine Learning course

Every model you will ever train is, deep down, just arithmetic on big grids of numbers. NumPy is the tool for those grids. It is so central that nearly every machine learning library is built on top of it. Learn a handful of ideas now, and the rest of this course will feel far easier.

NumPy in Ten Minutes
From the video: NumPy in Ten Minutes
  • Models are arithmetic on grids
  • NumPy is the foundation
  • A few ideas go a long way

Your first array

Here is an array of incomes. It looks like a list, but it is far more powerful. We can ask for the mean, or the maximum, directly. Now look at the next line. We multiply the whole array by ten thousand, with no loop at all, and every value changes. And the last line is a mask. We ask for only the values above four, and NumPy filters them for us. No loops. That idea is called vectorisation.

array.pypython
import numpy as np

incomes = np.array([3.2, 5.1, 8.3, 2.7, 6.4])

print("Mean:", incomes.mean())
print("Max: ", incomes.max())
print("In dollars:", incomes * 10000)
print("Above 4:", incomes[incomes > 4])
Output
Mean: 5.140000000000001
Max:  8.3
In dollars: [32000. 51000. 83000. 27000. 64000.]
Above 4: [5.1 8.3 6.4]

Why it matters: speed

Why not just use Python lists? Let's race them. We add up the squares of five million numbers using a normal Python loop, and time it. Then we do the same with a NumPy array. Run it, and look at the difference. NumPy is many times faster, because the heavy work happens in compiled code. When your data has millions of rows, this difference decides whether your model trains in seconds, or in hours.

race.pypython
import numpy as np
import time

numbers = [float(n) for n in range(5_000_000)]
start = time.time()
total = sum(n * n for n in numbers)
print(f"Python loop: {time.time() - start:.4f} s")

array = np.array(numbers)
start = time.time()
total = (array * array).sum()
print(f"NumPy array: {time.time() - start:.4f} s")
Output
Python loop: 0.2380 s
NumPy array: 0.0167 s

Rows and columns

Machine learning data is usually a two dimensional array. Each row is an example, and each column is a feature. Here are three districts, with income and house age. Shape says three rows, and two columns. Square brackets get a single row. The colon means every row, so colon comma one gets the second column. And axis zero means work down the columns, so we get the mean of each feature. This one is worth pausing on, because you will see it everywhere.

grid.pypython
import numpy as np

X = np.array([[3.2, 41],
              [8.3, 21],
              [5.6, 52]])

print("Shape:", X.shape)
print("First row:", X[0])
print("Second column:", X[:, 1])
print("Mean of each column:", X.mean(axis=0))
Output
Shape: (3, 2)
First row: [ 3.2 41. ]
Second column: [41. 21. 52.]
Mean of each column: [ 5.7 38. ]

Summary

Arrays, vectorisation, and rows and columns. That is most of what you need from NumPy. But arrays only hold numbers, and they have no column names. For real, messy tables, you need something friendlier. Next, pandas.

Key takeaways
  • Arrays do maths without loops
  • Vectorised code is far faster
  • Rows are examples, columns are features
Next up: pandas for Real Data — tables, filters, and groups