Advanced·15 min·datascience · numpy · performance
NumPy vectorization
NumPy vectorization: think in arrays
The #1 rule of numerical Python: if you're writing a for-loop over numbers, you're doing it wrong.
What vectorization means
An ufunc (universal function) operates on whole arrays in C, not Python. a + b where both are arrays does the addition element-wise, in a single tight C loop.
The performance story
- Python loop: interpreter overhead per iteration, ~50 ns each.
- NumPy op: compiled C, ~1 ns per element + minimal setup.
- Typical speedup: 20–200× for pure numerical work.
Broadcasting: shapes that don't match, work anyway
NumPy virtually stretches smaller arrays across bigger ones:
- Scalar + array: scalar reaches every cell.
- Row (
1×n) + column (m×1): producesm×n. - Rules: shapes match right-to-left, dim of 1 stretches, missing dims prepended as 1.
Boolean masking
a[a > 2] returns only elements matching. The killer feature for filtering.
Cheat sheet
np.zeros,np.ones,np.arange,np.linspace— construct.a.reshape(2, 3)— same data, new shape.a.sum(),.mean(),.std(),.max(),.argmax()— reductions.np.where(cond, if_true, if_false)— vectorized ternary.np.dot(a, b)/a @ b— matrix multiply.
Try it
- Compute Euclidean distance between two random 100k vectors — with a loop, then vectorized. Time both.
- Given a 2D array of grades, mark every value
< 40as 0 usingnp.where.