Vectorised Maths & Handy NumPy Functions
Do maths on a whole array in one line — no loops — and get instant summaries like mean and max.
What you will learn
- Add and combine arrays element-by-element
- Use summary functions (mean, sum, max)
- Understand “vectorised” operations
Vectorised = the whole array at once
Doing maths on an entire array in one line, with no loop, is called a vectorised operation. It is shorter to write and far faster to run. This is NumPy’s superpower.
Think of it like a checkout queue. With a Python list you ring up each item one at a time, in a loop. With a NumPy array it is as if every item is scanned at once — same result, far less waiting.
Two arrays of the same size combine element by element — the first with the first, the second with the second, and so on.
import numpy as np
prices = np.array([100, 200, 300]) # before tax
tax = np.array([18, 36, 54]) # 18% tax on each
total = prices + tax # element-by-element, no loop!
print(total)Note: Output: [118 236 354] 100+18, 200+36, 300+54 — all done in one line. With plain lists you would need a for-loop to do this.
The same thing with a loop (so you see the difference)
To really feel why vectorising is nicer, here is the loop you would write without NumPy to add the same two lists. It does the identical job — just longer and slower.
prices = [100, 200, 300]
tax = [18, 36, 54]
total = []
for i in range(len(prices)): # walk every position by hand
total.append(prices[i] + tax[i])
print(total)Note: Output:
[118, 236, 354]
Same answer as prices + tax — but five lines instead of one, and much slower on big data. That one-line NumPy version is what “vectorised” means.
Instant summaries
NumPy gives you one-word answers about an array: its average, total, biggest and smallest. These are the questions you ask of data every single day.
scores = np.array([72, 85, 90, 65, 88])
print('mean :', scores.mean()) # average
print('sum :', scores.sum()) # total
print('max :', scores.max()) # highest
print('min :', scores.min()) # lowestNote: Output: mean : 80.0 sum : 400 max : 90 min : 65 Five numbers summarised in four lines. The class average is 80, the top score 90, the lowest 65.
A function applied to every number
Vectorising is not just for + and *. NumPy functions like np.sqrt also run on the whole array at once, returning a new array with the function applied to every item.
import numpy as np
areas = np.array([4, 9, 16, 25]) # square areas
sides = np.sqrt(areas) # square root of each, in one call
print(sides)Note: Output: [2. 3. 4. 5.] √4=2, √9=3, √16=4, √25=5 — all four in a single call, with no loop. Almost every NumPy function works this way.
| Function | Answers the question | Example result |
|---|---|---|
.mean() | What is the average? | 80.0 |
.sum() | What is the total? | 400 |
.max() / .min() | Highest / lowest? | 90 / 65 |
.std() | How spread out is it? | (see Stats unit) |
np.sqrt(a) | Square root of each item | […] |
Tip: You can compare a whole array at once too: scores > 80 returns [False True True False True]. This trick — a True/False array — is how you will filter data in Pandas.
Watch out: Two arrays must be the same size to add or multiply element-by-element. Adding a 3-item array to a 5-item one raises a shape error.
Q. What does np.array([2, 4, 6]).mean() return?
✍️ Practice
- Create an array of daily temperatures and print the mean, max and min.
- Make two arrays (prices and discounts) of the same length and subtract them to get final prices.
🏠 Homework
- Make an array of your last five quiz scores. Print the average, then print which scores were above the average using a comparison like
scores > scores.mean().