NumPy in Practice
Why NumPy exists (revisited, but concretely)
Python lists are great containers, but terrible calculators.
NumPy replaces loops over numbers with vectorized operations written in fast C.
This single idea explains 90% of NumPy.
Creating arrays (the right way)
From Python data
All elements must be the same type.
Built-in constructors (very common)
Ranges
Use:
arangefor integer stepslinspacefor exact spacing
Understanding shape and dimensions
Mental model:
- shape = rows × columns
- ndim = how many axes
Indexing and slicing (core skill)
1D arrays
2D arrays
This replaces nested loops cleanly.
Vectorized math (where NumPy shines)
Every operation applies element-wise.
Even comparisons:
Boolean masks (extremely important)
Boolean arrays let you select and modify data.
Filter:
Modify:
This replaces:
- conditionals inside loops
- temporary lists
Broadcasting (how shapes interact)
Broadcasting lets NumPy combine arrays of different shapes.
2D + 1D:
Rule:
Dimensions must either match or be 1.
If broadcasting feels magical — that’s normal. Use it intentionally, not cleverly.
Aggregations and statistics
On matrices:
Axis = what you collapse.
Reshaping data
Automatic dimension:
Transpose:
Flatten:
Reshaping is common in:
- ML pipelines
- image processing
- matrix math
Practical example 1: normalize sensor data
readings = np.array([120, 130, 125, 140, 135])
mean = readings.mean()
std = readings.std()
normalized = (readings - mean) / std
normalized
This would be ugly and slow with loops.
Practical example 2: tabular data analysis
Imagine rows = days, columns = sensors.
Daily average:
Sensor average:
Find hot days:
Reading and writing data
Text files
CSV
NumPy is strict; for messy data, pandas is better.
NumPy arrays vs Python lists (decision guide)
Use lists when:
- Data is small
- Mixed types
- Dynamic resizing
Use NumPy when:
- Numeric data
- Fixed shape
- Performance matters