Lesson 4 of 9 · 42 min · UG / diploma
NumPy — whole columns at once
A Python for-loop over 500,000 temperatures is correct and slow. NumPy stores a typed block of numbers (an ndarray) and runs the same add/multiply on every cell in compiled code. Sensors, images, audio and machine learning all speak this shape. You do not replace thinking — you replace the inner loop.
Numeric arrays
Same operation on thousands of numbers at once.
You will be able to
- ✓ Contrast a Python list with a NumPy array
- ✓ Use vectorised arithmetic and aggregations
- ✓ Know dtype and broadcasting at a reading level
Lists are boxes; arrays are a grid of one type
A Python list can hold a number, a string and another list. That flexibility costs speed: every item is a separate object. A NumPy array is one type (float64, int32) laid out in a rectangle. temps + 1 adds one to every cell. temps[temps >= 35] is a filter without an explicit loop.
Shape matters. A column of 1440 minute-readings is shape (1440,). A day’s worth of 10 sensors is (1440, 10). Images are often (height, width, 3) for RGB. Once you see shape, every later tensor in PyTorch is the same idea.
Vectorisation is the habit
Write the maths as if it applied to one number, then let NumPy apply it to the array. fahrenheit = celsius * 9 / 5 + 32 works on an array. A Python loop that appends each conversion is what you write in this sandbox — and what you should stop writing on a laptop once the array is large.
Aggregations: np.mean, np.max, np.std, np.sum. Axis=0 vs axis=1 is “down columns” vs “across rows”. Lab reports live here: mean temperature per sensor, not a mystery number from Excel.
Broadcasting and dtype
Broadcasting lets a 1-D array of 10 offsets add to a (1440, 10) table — each column gets its calibration. When it fails, the error is about shape. Read the shapes; do not randomly .reshape until it “runs”.
dtype=float32 halves memory versus float64. For money, prefer decimals or integer paise — binary floats are the reason 0.1 + 0.2 looks ugly. For sensors, float64 is the default and usually fine.
Where NumPy sits in an application
Load with pandas or np.loadtxt, compute with NumPy, plot with matplotlib, store summaries in SQLite. Do not persist a 2 GB array as a CSV of 12 million lines if you can use .npy or a database of aggregates.
NumPy is not a table with column names. Named columns are pandas. Mixing them is normal: df["celsius"].to_numpy().
Words that matter
- ndarray
- NumPy’s N-dimensional array: one dtype, a shape, vectorised ops.
- Vectorisation
- Expressing an operation on the whole array instead of a Python loop.
- dtype
- The element type (int32, float64, …).
- Broadcasting
- Stretching smaller arrays to match a larger shape during arithmetic.
Common mistakes
Avoid: A Python for-loop over a million floats “because it is clearer”.
Do this: Write the vectorised line; it is clearer to people who read NumPy.
Avoid: Using NumPy arrays as a general list of mixed dicts.
Do this: Heterogeneous rows belong in pandas or a list of dicts.
On a full Python install — numpy
The standard numeric array library. pandas, scikit-learn, OpenCV and most science code expect it.
pip install numpy
Calibrate a column of °C, flag heat, report mean.
Real library code (not run in this browser sandbox)
import numpy as np
celsius = np.array([29.0, 36.5, 33.0, 40.0, 31.2])
calibrated = celsius + 0.4
hot = calibrated[calibrated >= 35]
print("hot", hot)
print("mean", calibrated.mean())
print("peak", calibrated.max())
Example program — The same maths with a list (sandbox analog)
This is the loop NumPy replaces. Add 1 °C of calibration, keep values >= 35.
Edit the example, press Run, then Build if you want a compile check.
build
Press Build to compile.
Your turn — Scale a column
readings = [10, 20, 30]. Build scaled where each value is times 2. Print so 40 appears (from 20×2).
Edit the example, press Run, then Build if you want a compile check.
build
Press Build to compile.
Self-assessment
Check your understanding before you mark the lesson complete.
Progress is stored in a browser cookie on this device.