Syllabus Lesson 76 of 239 · Data Foundations: numpy & pandas
Data Foundations: numpy & pandas

Vectorization and Broadcasting

Arithmetic on arrays happens elementwise, with no loop:

import numpy as np
a = np.array([1, 2, 3])
b = np.array([10, 20, 30])
print(a + b)     # [11 22 33]
print(a * b)     # [10 40 90]
print(b / a)     # [10. 10. 10.]

Broadcasting is how numpy stretches a smaller shape to fit a larger one. A single number (a scalar) is applied to every element:

a = np.array([1, 2, 3])
print(a + 100)   # [101 102 103]
print(a ** 2)    # [1 4 9]

It also works across rows. A 1D row of length 3 broadcasts down every row of a 2x3 grid:

grid = np.array([[1, 2, 3],
                 [4, 5, 6]])
bonus = np.array([10, 20, 30])
print(grid + bonus)
# [[11 22 33]
#  [14 25 36]]

Comparisons are vectorized too and give a boolean array, which we will use for filtering soon:

scores = np.array([55, 90, 70, 40])
print(scores >= 60)   # [False  True  True False]

Vectorized code is shorter and far faster than the equivalent Python loop.

Your turn

Start from celsius = np.array([0, 10, 20, 30]). Using vectorized math (no loop), convert to Fahrenheit with the formula F = C * 9/5 + 32 and store it in fahrenheit. Then build a boolean array warm that is True where fahrenheit is at least 60.

Spotted a problem in this lesson? Report it

Code · runs in your browser
Output