NumPy Maths and Broadcasting
🎧 Focus sounds
Soft beat sounds that many learners like while reading. Pick one, press Play, and keep studying.
Keep the volume low. These sounds are for comfort while studying, not a medical treatment. If you have epilepsy or are sensitive to pulsing sounds, use the background noise only.
In this lesson
You’ve seen that marks + 5 adds 5 to every element. This lesson explains the full set of maths NumPy can do on arrays, and broadcasting, the rule that decides how arrays of different shapes are combined.
Element-wise maths#
When two arrays have the same shape, maths is done position by position:
import numpy as np
price = np.array([100, 250, 40])
qty = np.array([2, 1, 5])
print(price * qty)
print((price * qty).sum())
Output:
[200 250 200]
650
That’s a whole shopping bill in two lines: each price times its quantity, then the total.
Universal functions (ufuncs)#
NumPy has fast versions of common maths functions that work on every element:
import numpy as np
x = np.array([1, 4, 9, 16])
print(np.sqrt(x))
print(np.round(np.log10(x), 2))
print(np.abs(np.array([-3, 2, -1])))
Output:
[1. 2. 3. 4.]
[0. 0.6 0.95 1.2 ]
[3 2 1]
Others you’ll use: np.exp, np.sin, np.cos, np.power, np.maximum, np.clip.
Broadcasting: different shapes#
What if the shapes don’t match? Adding a single number to an array already worked, because NumPy stretched the number to every position. The same idea works for rows and columns. Watch how a row is added to a whole table:
import numpy as np
m = np.array([[1, 2, 3],
[4, 5, 6],
[7, 8, 9]])
row = np.array([10, 20, 30])
print(m + row)
Output:
[[11 22 33]
[14 25 36]
[17 28 39]]
The rule: compare shapes from the right. Two sizes are compatible when they’re equal, or when one of them is 1 (or missing). The size-1 side is stretched.
| Shapes | Works? | Why |
|---|---|---|
| (3, 3) + (3,) | ✅ | the row is stretched down 3 times |
| (3, 3) + (3, 1) | ✅ | the column is stretched across |
| (3, 3) + (2,) | ❌ | 3 and 2 don’t match and neither is 1 |
A real example: give each subject its own weight in a final score.
import numpy as np
scores = np.array([[80, 70, 90],
[60, 85, 75]])
weights = np.array([0.5, 0.3, 0.2])
print(scores * weights)
print((scores * weights).sum(axis=1))
Output:
[[40. 21. 18. ]
[30. 25.5 15. ]]
[79. 70.5]
Each row is one student; .sum(axis=1) adds across each row, giving one final score per student.
Error to recognise
ValueError: operands could not be broadcast together with shapes (3,3) (2,) means the shapes break the rule above. Print both .shape values and check them from the right.
Normalising data (a machine learning favourite)#
Models learn better when every column is on a similar scale. Broadcasting does it in one line:
import numpy as np
data = np.array([[150, 50000],
[180, 80000],
[165, 65000]])
scaled = (data - data.min(axis=0)) / (data.max(axis=0) - data.min(axis=0))
print(scaled)
Output:
[[0. 0. ]
[1. 1. ]
[0.5 0.5]]
data.min(axis=0) gives the minimum of each column, shape (2,); broadcasting subtracts it from every row. Now height and salary are both between 0 and 1.
Summary#
- Same shapes: maths is element by element.
- Ufuncs like
np.sqrtandnp.logwork on every element, fast. - Broadcasting stretches size-1 (or missing) dimensions; compare shapes from the right.
- It powers everyday tasks like weighted scores and scaling features.
Quick quiz
3 questions. Check what stuck and earn 2 XP for each right answer.
Progress is saved in this browser. Create a free account to keep it on every device.
Get new lessons in your inbox
One email when we publish a new tutorial or guide. No spam, unsubscribe in one click.