Module 16 · External Libraries and APIs — topic 5 of 12
NumPy introduction
Fast numerical arrays with NumPy: creating arrays, vectorised arithmetic, shapes and reshaping, axis-wise aggregates, boolean masks, broadcasting, and why NumPy is much faster than lists for numbers.
Learning objectives
- Create and inspect NumPy arrays
- Use vectorised operations and aggregates along axes
- Filter data with boolean masks
Definition
NumPy is the fundamental package for numerical computing in Python. Its ndarray stores numbers of one type in a compact block of memory and performs operations on whole arrays at C speed.
Introduction
Adding 10 to a million numbers in a Python loop takes noticeable time; in NumPy it's a single, almost instant expression: arr + 10.
Why it matters
pandas, Matplotlib, SciPy, scikit-learn and most machine-learning libraries are built on NumPy arrays. Understanding them unlocks the whole data ecosystem.
Detailed explanation
Install with python -m pip install numpy and import it as np by convention.
Create arrays from lists with np.array, or with helpers: np.arange, np.linspace, np.zeros, np.ones.
Every array has a dtype (element type, such as int64 or float64) and a shape (dimensions, such as (2, 3)). reshape changes the shape without copying data when possible.
Arithmetic is element-wise and vectorised: a * 10, a + b, a ** 2 operate on every element without a Python loop.
Aggregates such as sum, mean, min and max work on the whole array or along an axis: axis=0 goes down columns, axis=1 across rows.
Comparisons produce boolean arrays, which can be used as masks: arr[arr > 80] selects matching elements.
Broadcasting lets arrays of different but compatible shapes combine, such as adding a row of per-subject bonuses to every row of a marks table.
np.clip, np.where and many other functions apply common operations to whole arrays.
Syntax
- Arrays hold one dtype.
- Operations are vectorised.
- Masks filter elements.
Code examples
Array basics
[10 20 30 40] [2 4 6 8] 2.5 int64 (4,)
Requires NumPy.
Shapes and axes
[[1 2 3] [4 5 6]] [5 7 9] [ 6 15] (3, 2)
Requires NumPy.
More examples
Analysing a marks table
[70.8 79.5 82.2] [82.7 74.7 91. 61.7] [ True False True False] 2 100 9 [0. 0.25 0.5 0.75 1. ]
Requires NumPy.
Step-by-step explanation
- mean(axis=0) averages each column (subject); mean(axis=1) averages each row (student).
- The boolean array top selects the rows of students averaging 80 or more.
- Adding 5 to every mark and clipping at 100 happens in one vectorised step; summing a boolean array counts the True values.
Expected output
Running “Analysing a marks table” prints exactly:
Practical applications
- Image processing (images are arrays of pixels).
- Scientific simulations and machine-learning features.
Best practices
- Think in whole-array operations.
- Check shape and dtype when results look wrong.
- Use masks instead of if statements inside loops.
Common mistakes
Looping over arrays
Avoid
Prefer
Python loops lose NumPy's speed advantage.
Using and/or with arrays
Avoid
Prefer
and/or need single True/False values; use & and | with parentheses for element-wise logic.
Important notes
- Integer arrays have fixed size and can overflow silently, unlike Python ints.
Key takeaways
- ndarrays are fast typed arrays.
- Operations and aggregates are vectorised, optionally per axis.
- Boolean masks filter data.
Try it yourself
- 1Create a 5×5 array of the numbers 1–25 and print its diagonal and the sum of each column.
- 2Convert a list of Celsius temperatures to Fahrenheit with one NumPy expression.
Finished reading? Mark it complete to update your dashboard.
Suggested practice
Test yourself across the whole course in the Python practice bank.