English for NumPy Developers
Vocabulary for developers working with NumPy — arrays, broadcasting, vectorization, and dtypes — for teams discussing numerical Python code in English.
NumPy conversations lean heavily on precise shape and type vocabulary, because most NumPy bugs are shape mismatches or silent type coercions rather than logic errors. Saying exactly what shape or dtype you expect — instead of “the array is wrong” — is what makes these bugs fast to diagnose.
Arrays and Shape
ndarray — NumPy’s core n-dimensional array type: a fixed-size, homogeneously-typed grid of values, which is what makes vectorized operations fast.
“This isn’t a Python list anymore once it’s an ndarray — that’s why slicing and math behave so differently.”
Shape — the tuple describing an array’s dimensions, e.g. (3, 224, 224) for a 3-channel image.
“Print the shape before the matmul — I’d bet this is a (3, 224, 224) vs (224, 224, 3) channel-order mismatch.”
Axis — a specific dimension of an array along which an operation (like a sum or mean) is applied.
“You want axis=1 here, not axis=0 — you’re summing across columns, not rows.”
Broadcasting and Vectorization
Broadcasting — NumPy’s rule set for automatically expanding arrays of compatible but different shapes so an operation can apply element-wise without explicit loops.
“You don’t need to tile that array manually — broadcasting handles a
(3,)array against a(100, 3)array automatically.”
Vectorization — expressing a computation as array-wide operations instead of Python-level loops, which NumPy executes in compiled C rather than the Python interpreter.
“Replace that for-loop with a vectorized operation — same result, but roughly 50x faster on an array this size.”
View vs. copy — a view shares the same underlying memory as the original array (so modifying it modifies the original), while a copy is fully independent.
“That slice is a view, not a copy — modifying it in place just silently corrupted the original array you passed in.”
Types and Precision
dtype — the data type of every element in an array (e.g. float32, int64), fixed for the whole array unlike a Python list.
“Check the dtype before you divide — integer division on an int64 array truncates instead of giving you the fraction you expected.”
Type promotion / coercion — NumPy silently upcasting values to a compatible dtype when combining arrays of different types, which can hide precision loss.
“Mixing float32 and float64 here silently promotes everything to float64 — that’s probably why memory usage doubled after this change.”
NaN propagation — the way a single NaN value spreads through most arithmetic operations, silently turning valid results into NaN downstream.
“One missing value became NaN, and NaN propagated through the mean calculation — that’s why the whole row came back NaN, not just the one cell.”
Common Mistakes
- Saying “the shapes don’t match” without printing the actual shapes — precision here saves several rounds of back-and-forth.
- Modifying a slice and being surprised the original array changed, without realizing the slice was a view, not a copy.
- Assuming a
NaNin the output means a bug in the current line, when it’s often propagated from missing data much earlier in the pipeline.
Practice Exercise
- Explain, in two sentences, the difference between a NumPy view and a copy to someone debugging unexpected mutation.
- Write a short code review comment recommending a vectorized replacement for a Python for-loop over an array.
- Draft a message diagnosing an unexpected
NaNresult as propagation from upstream missing data.