20 / 22 · Scientific Python
NumPy arrays
The NumPy array as one block of memory described by a dtype, a shape and strides: views against copies, fixed-width types and overflow, vectorized ufuncs and why they are fast, broadcasting, indexing, reductions, the matrix product and seeded random numbers.
On this page 12
The previous note spread work over threads and processes to use more of the machine. For numerical work a larger gain comes first, before any parallelism: running the loop in compiled code instead of in the interpreter. A Python list of a million numbers is a million separate objects, and a loop over it pays the bytecode loop, a type check and a reference count for every element. NumPy stores the same numbers as one block of raw machine values and runs each operation as a loop in C over that block. This note explains what a NumPy array is in memory, because that one picture predicts when an operation is a free view and when it copies, why integers can overflow, why vectorized code is fast, and how arrays of different shapes combine. Everything the machine-learning courses compute with, from the normal equations to a batch of images, is built on it.
Conventions. NumPy 2.5.4 on CPython 3.14.7, Apple M1, macOS; import numpy as np is assumed in every snippet. Arrays print with array(...) in the REPL and without it under print.
Baseline: a list of numbers#
The obvious way to hold a million measurements is a list of floats, and the obvious way to compute with them is a loop:
def sum_of_squares(xs):
total = 0.0
for v in xs:
total += v * v
return total
The lists note showed what the list is: an array of pointers, each to a separate float object of 24 bytes somewhere on the heap. A list of 1000 floats takes 8056 bytes for the pointers plus 24 000 for the objects; the same numbers as a NumPy array take 8000 bytes, one 8-byte double each. And each iteration of the loop executes eight bytecode instructions, each of which checks the types of its operands, allocates a new float object for each result and adjusts reference counts (How CPython runs code). Measured on the setup above, the loop costs 19.6 ns per element, the same for 1000 elements and for a million: the cost is the interpreter’s, not the arithmetic’s. The processor can multiply and add two doubles in well under a nanosecond.
The performance note measured the same gap from the other side and named its remedy: move the loop out of Python. NumPy is that remedy for numbers, and to use it well we need its model of data.
What an array is#
Notation. An array with axes has shape and strides in bytes; is the size of one element in bytes (itemsize). The element with index sits at
The strides are what make one block of memory look like a matrix. For a freshly created array NumPy chooses them in one of two orders:
- C order (row-major, the default): the last index changes fastest. Moving along the last axis steps one element, , and each earlier axis steps over a whole block of the axis after it, .
- Fortran order (column-major,
order="F"): the first index changes fastest, and .
For a array of 8-byte integers, C order gives and : the next column is 8 bytes on, the next row 32. Fortran order gives and .
An array is a block of memory, a shape and strides
The REPL shows the description directly:
>>> a = np.arange(12).reshape(3, 4)
>>> a
array([[ 0, 1, 2, 3],
[ 4, 5, 6, 7],
[ 8, 9, 10, 11]])
>>> a.dtype, a.shape, a.strides, a.itemsize, a.nbytes
(dtype('int64'), (3, 4), (32, 8), 8, 96)
>>> f = np.asfortranarray(a)
>>> f.strides, f.flags["C_CONTIGUOUS"], f.flags["F_CONTIGUOUS"]
((8, 24), False, True)
>>> a[2, 1], f[2, 1]
(np.int64(9), np.int64(9))
Line by line: np.arange(12) creates 12 consecutive integers, and reshape(3, 4) describes the same 96 bytes as three rows of four. The default integer dtype is int64 on 64-bit platforms. np.asfortranarray copies the data into column-major order: the values are the same, the bytes are arranged differently, and the strides say how. Indexing a[2, 1] uses the formula: bytes from the start in a, in f. A single element comes back as a NumPy scalar, np.int64(9); .item() converts it to a Python int.
Why the order matters. Memory is read in cache lines, 64 bytes on most x86 processors and 128 on the M1 used here (caches). A loop, or a NumPy operation, that walks along the axis with the small stride uses every byte of each line it loads; one that walks along the axis with the large stride loads a line for each element. NumPy’s own operations pick a good order where they can, but the order still matters when data are handed to other code: C and PyTorch expect row-major data, Fortran libraries such as LAPACK column-major.
Views and copies#
Because an array is a description of memory, many operations only write a new description.
The rule. These return a view, a new array object that shares the data block:
- basic slicing with integers and
start:stop:step(a[1],a[:, 1],a[::2, ::3]): the start moves and the strides are multiplied by the step; .Tandtranspose: the shape and the strides are reversed;reshapeandravel, when the new shape can be described by strides over the same memory; otherwise they copy.
These always return a copy, a new block: fancy indexing with a list or array of integers, boolean indexing, .copy(), flatten(), and arithmetic (a + 1 is a new array). np.shares_memory(x, y) tells whether two arrays overlap in memory.
>>> a = np.arange(12).reshape(3, 4)
>>> col = a[:, 1]
>>> col, col.strides, np.shares_memory(a, col)
(array([1, 5, 9]), (32,), True)
>>> a[::2, ::3], a[::2, ::3].strides
(array([[ 0, 3],
[ 8, 11]]), (64, 24))
>>> a.T.strides, np.shares_memory(a, a.T)
((8, 32), True)
>>> np.shares_memory(a, a.reshape(4, 3)), np.shares_memory(a, a.T.reshape(12))
(True, False)
>>> np.shares_memory(a, a[[0, 2]]), np.shares_memory(a, a[a % 2 == 0])
(False, False)
The column a[:, 1] starts 8 bytes in and steps 32 bytes, one row, per element; nothing was copied. a[::2, ::3] takes every second row and every third column: strides and . The transpose reverses the strides to (8, 32). a.reshape(4, 3) reads the same 12 numbers in the same order, so it is a view. a.T.reshape(12) asks for the transpose’s elements in row order, 0, 4, 8, 1, …: no single stride walks the original block in that order, so NumPy copies. Since NumPy 2.1, np.reshape(x, shape, copy=False) raises ValueError: Unable to avoid creating a copy while reshaping. instead of copying silently, which is a useful assertion in code that must not copy. The index list [0, 2] and the boolean mask can pick any elements in any order, which no strides can describe: they copy.
The mistake. A view shares its data, so writing through it changes the original:
>>> a = np.arange(12).reshape(3, 4)
>>> row = a[1]
>>> row[0] = 100
>>> a[1]
array([100, 5, 6, 7])
This is the aliasing of the values note one level down: two array objects, one block of memory. It differs from lists, whose slices are copies. Two consequences follow. A function that receives a slice can change the caller’s array, so take .copy() when a function must not. And a small view keeps the whole block alive: big[:10] of a 1 GB array holds the gigabyte until the view is gone, so big[:10].copy() is the way to keep only ten elements.
dtypes and overflow#
The rule. Every element of an array has the same dtype, a fixed-width machine type: int8 to int64, uint8 to uint64, float16, float32, float64, complex128, bool, and fixed-width strings and Python objects (object, which brings back the cost of Python objects). Arithmetic on fixed-width integers wraps around modulo without an error, exactly as the processor does. Python’s own int never overflows (values note); a NumPy integer does.
>>> x = np.array([100, 120, 127], dtype=np.int8)
>>> x + 1
array([ 101, 121, -128], dtype=int8)
>>> x * 2
array([-56, -16, -2], dtype=int8)
>>> x + 200
Traceback (most recent call last):
...
OverflowError: Python integer 200 out of bounds for int8
>>> np.array([1, 2], dtype=np.uint8) - 3
array([254, 255], dtype=uint8)
does not fit in int8, whose range is to , and wraps to . becomes . No warning is raised for arrays; checking every element would cost as much as the arithmetic. The third line shows the promotion rule of NumPy 2 (NEP 50): a Python number in an expression is “weak” and takes the array’s dtype, so x + 1 stays int8, and a Python integer that does not fit raises OverflowError instead of wrapping. A NumPy scalar or array is “strong”: np.float32 array plus np.float64(1) gives float64, while plus the Python float 1.0 stays float32.
Where it bites: images are stored as uint8 (0 to 255), and img1 - img2 or img * 2 wraps instead of going negative or saturating. Convert first, img.astype(np.int16) or img.astype(np.float32) / 255.
float32 against float64. A float64 has a 53-bit significand, about 16 decimal digits, and machine epsilon ; a float32 has 24 bits, about 7 digits, and epsilon (rounding, ulp and machine epsilon). Half the bytes means half the memory traffic and twice the SIMD lanes, which is why deep learning computes in float32 and narrower (narrow formats). The price shows up in long sums:
>>> v = np.full(10_000_000, 0.1, dtype=np.float32)
>>> v.sum(), np.cumsum(v)[-1], v.sum(dtype=np.float64)
(np.float32(1.0000001e+06), np.float32(1.087937e+06), np.float64(1000000.0149011612))
>>> np.float32(16777216) + np.float32(1)
np.float32(1.6777216e+07)
Ten million copies of 0.1 should sum to . v.sum() is close because NumPy sums in pairs, a tree of additions whose rounding errors grow like rather than . np.cumsum must add one element at a time, left to right, and once the running total is near each added 0.1 is rounded to a multiple of the total’s ulp, there: the result is 9% too large. The last line is the reason: at the gap between consecutive float32 values is 2, so adding 1 changes nothing. Accumulate in float64 (dtype=np.float64) when a sum has millions of terms.
Vectorized operations: ufuncs#
A universal function (ufunc) applies one scalar operation to every element: np.add (the + operator), np.multiply, np.sqrt, np.exp, comparisons and about a hundred more. x * x calls np.multiply(x, x).
The rule. A ufunc takes arrays of compatible shapes (the broadcasting rule below), chooses an inner loop for their dtypes, allocates the output and runs the loop over all elements. Each ufunc also has methods that build on the same loop: reduce (np.add.reduce(x) is x.sum()), accumulate (np.cumsum) and outer.
Why it is fast. Three costs of the Python loop disappear at once:
- Dispatch happens once per array, not once per element. The interpreter executes one call; the type check and the choice of the C loop are made once.
- The inner loop is typed C over raw values. There are no objects to allocate, no reference counts, no type checks inside it. It walks memory with a fixed stride, which the cache and the prefetcher handle well.
- The inner loop uses SIMD instructions. On this M1 NumPy’s loops are compiled for the NEON/ASIMD vector registers, which hold two
float64or fourfloat32values and operate on all of them in one instruction (data-level parallelism).
The price is the fixed overhead of a call, about half a microsecond here, and a temporary array for every intermediate result: (x * x).sum() writes the whole array x * x to memory and reads it back.
Measured: a loop against a vector#
The sum of squares four ways, on the setup above, for arrays of random floats from 10 to a million elements: the Python loop over a list, the same loop over the NumPy array, (x * x).sum(), and x @ x, the dot product, which NumPy hands to the BLAS library (Apple Accelerate in this build). Each number is the best of five repetitions, divided by the number of elements.
| elements | loop over a list | loop over an array | (x * x).sum() | x @ x |
|---|---|---|---|---|
| 10 | 0.26 µs | 1.4 µs | 0.62 µs | 0.53 µs |
| 1 000 | 19.3 µs | 99 µs | 1.0 µs | 0.68 µs |
| 1 000 000 | 19.6 ms | 101 ms | 0.53 ms | 0.17 ms |
A Python loop against vectorized code
What the measurement shows:
- For a million elements the vectorized sum is 37 times faster than the list loop and
x @ x113 times. The list loop’s 19.6 ns per element is flat: the interpreter’s cost does not depend on the size. - For ten elements the list loop wins. The vector pays its fixed overhead of about 0.5 µs, and there are too few elements to recover it. Vectorize work on large arrays, not on three numbers.
- A Python loop over a NumPy array is the slowest of all, five times slower than over a list: every
for v in xcreates a NumPy scalar object from the raw value, and arithmetic on NumPy scalars goes through more machinery than on Python floats. Iterating over an array element by element gives up everything the array offers. (x * x).sum()writes a temporary arrayx * xand reads it back. At a million elements that temporary is 8 MB and still fits in the M1’s shared cache: the 0.53 ns in the table is one run, and repeated runs gave 0.38 to 0.39 ns, the same as at 100 000. The cost per element rises clearly only at ten million elements, to about 0.47 ns, when the 80 MB temporary goes to memory and back.x @ xcomputes the same number in one pass with no temporary.
These are measurements on one machine; the debugging and profiling note explains how to time code reliably. On another machine the ratios differ while the shape of the picture does not.
Broadcasting#
Arithmetic between arrays of different shapes is common: subtract the mean of each column from a matrix, add a bias vector to every row of a batch. NumPy defines it by one rule instead of requiring copies to equal shapes.
The rule (NumPy, Broadcasting). To combine two arrays:
- Line up their shapes on the right. If one has fewer axes, pad its shape on the left with 1s.
- Compare the sizes axis by axis. Two sizes are compatible when they are equal or one of them is 1. Any other pair raises
ValueError. - The result has, on each axis, the larger of the two sizes.
- An operand of size 1 on an axis is used as if repeated along it. Nothing is copied: the repeated axis gets stride 0, so every position reads the same element.
Worked: a column of shape (3, 1) plus a row of shape (4,).
Broadcasting: (3, 1) + (4,) → (3, 4)
- Line up on the right:
(3, 1)and(4,); the row has one axis fewer. - Pad the row on the left:
(4,)becomes(1, 4). The pairs are with and with : compatible on both axes. - Stretch: the column is used as if it had four identical columns, the row as if it had three identical rows. Both read their own few elements with stride 0 along the stretched axis.
- Add elementwise. The result has shape
(3, 4): entry is .
>>> col = np.array([[0], [10], [20]])
>>> row = np.array([1, 2, 3, 4])
>>> col + row
array([[ 1, 2, 3, 4],
[11, 12, 13, 14],
[21, 22, 23, 24]])
>>> np.broadcast_to(row, (3, 4)).strides
(0, 8)
>>> np.broadcast_shapes((8, 1, 6, 1), (7, 1, 5))
(8, 7, 6, 5)
>>> np.ones(3) + np.ones(4)
Traceback (most recent call last):
...
ValueError: operands could not be broadcast together with shapes (3,) (4,)
The everyday use is centering the columns of a data matrix of shape : X.mean(axis=0) has shape , which lines up with the last axis of , so X - X.mean(axis=0) subtracts each column’s mean from that column. Centering the rows needs the means as a column: X.mean(axis=1, keepdims=True) has shape .
The mistake. Broadcasting never asks whether you meant it. Two cases are silent:
>>> X = np.array([[1., 2., 3.], [4., 5., 6.], [7., 8., 18.]])
>>> X - X.mean(axis=1)
array([[-1., -3., -8.],
[ 2., 0., -5.],
[ 5., 3., 7.]])
>>> y = np.array([1., 2., 3.])
>>> pred = np.array([[1.], [2.], [3.]])
>>> (y - pred).shape, ((y - pred) ** 2).mean()
((3, 3), np.float64(1.3333333333333333))
The row means [2, 5, 11] have shape (3,), which lines up with the columns: the first column has 2 subtracted, the second 5, the third 11, which is not what “subtract each row’s mean” means. For a non-square the shapes would clash and raise; for a square one the bug is silent. The second case is the classic error in a loss: a model returns predictions of shape , the targets have shape , and y - pred is an matrix of every target minus every prediction. The predictions here are perfect, and the “mean squared error” is 1.33. Check shapes with assert pred.shape == y.shape, or flatten with pred[:, 0]. PyTorch uses the same broadcasting rules and has the same trap.
Indexing: basic, fancy and boolean#
Three kinds of index select elements, and the kind decides whether you get a view.
- Basic: integers, slices,
None(np.newaxis, a new axis of size 1) and.... Returns a view, as above. - Fancy (integer arrays):
a[[2, 0]]takes rows 2 and 0 in that order;a[[0, 1, 2], [3, 2, 1]]takes the elements . Returns a copy. - Boolean (a mask of the same shape):
a[a > 5]returns the elements where the mask is true, as a 1-D copy.
>>> a = np.arange(12).reshape(3, 4)
>>> a[[2, 0]]
array([[ 8, 9, 10, 11],
[ 0, 1, 2, 3]])
>>> a[[0, 1, 2], [3, 2, 1]]
array([3, 6, 9])
>>> a[a > 8]
array([ 9, 10, 11])
>>> a[a > 8] = 0
>>> a
array([[0, 1, 2, 3],
[4, 5, 6, 7],
[8, 0, 0, 0]])
Reading through a fancy or boolean index copies, but assigning through one writes into the original: a[a > 8] = 0 is a single call to a.__setitem__, which writes in place. The trap is to combine the two: a[a > 8][0] = 99 first makes a copy with __getitem__, then changes the copy, and a is untouched. The same chained-indexing trap returns in pandas. The ML courses use fancy indexing to look up rows of a table by index: the Machine Learning course’s target encoding indexes an array of per-category means with an array of category numbers.
Reductions and axis#
A reduction combines many elements into one: sum, mean, std, min, max, argmax, any, all.
The rule. Without axis, the reduction runs over all elements. With axis=k, it runs along axis , and axis disappears from the shape: an array of shape reduced with axis=0 has shape , one result per column; with axis=1, shape , one per row. keepdims=True keeps the reduced axis with size 1, so the result broadcasts back against the input.
>>> a = np.arange(12).reshape(3, 4)
>>> a.sum(), a.sum(axis=0), a.sum(axis=1)
(np.int64(66), array([12, 15, 18, 21]), array([ 6, 22, 38]))
>>> a.sum(axis=1, keepdims=True).shape
(3, 1)
>>> a.argmax(axis=1)
array([3, 3, 3])
A way to remember it: axis=0 collapses the rows, so you get one number per column. In the ML courses has one row per object, and X.mean(axis=0) is the vector of feature means; a batch of logits has shape (batch, classes), and logits.argmax(axis=1) is the predicted class of each object.
The matrix product @#
* is elementwise. The matrix product is the operator @, which calls np.matmul.
The rule. For 2-D arrays of shapes and , A @ B has shape and entry ; the inner sizes must match. A 1-D first operand is treated as a row, a 1-D second operand as a column, and the added axis is removed from the result, so A @ x of shapes and is a vector of shape and x @ x is a scalar. With more than two axes, the last two are the matrices and the leading ones are a batch, broadcast by the rule above.
>>> A = np.array([[1, 2], [3, 4]])
>>> x = np.array([1, 1])
>>> A @ x, A * x
(array([3, 7]), array([[1, 2],
[3, 4]]))
>>> (np.ones((5, 3, 4)) @ np.ones((4, 2))).shape
(5, 3, 2)
>>> np.ones((2, 3)) @ np.ones((2, 3))
Traceback (most recent call last):
...
ValueError: matmul: Input operand 1 has a mismatch in its core dimension 0, with gufunc signature (n?,k),(k,m?)->(n?,m?) (size 2 is different from 3)
A @ x is , the matrix applied to the vector; A * x broadcasts x along the rows and multiplies elementwise, which is a different operation that also runs without error. The batched product multiplies each of the five matrices by the same one. The error message spells out the rule in its signature: the of the first operand must equal the of the second. What the product means, a linear map and three readings of , is the linear algebra of the Mathematics course; np.linalg.solve(A, b) solves the system and is what the least-squares note calls on .
Random numbers#
The rule. Create one generator with a seed and draw everything from it: rng = np.random.default_rng(seed). The same seed and the same NumPy version give the same numbers. The generator holds its state, the PCG64 bit generator by default, so successive calls continue the stream instead of repeating it.
>>> rng = np.random.default_rng(42)
>>> rng.random(3)
array([0.77395605, 0.43887844, 0.85859792])
>>> rng.integers(0, 10, size=5)
array([0, 6, 2, 0, 5])
>>> np.random.default_rng(42).random(3)
array([0.77395605, 0.43887844, 0.85859792])
rng.random(3) draws three uniform numbers in ; rng.integers(0, 10, size=5) five integers from 0 to 9; rng.normal(mean, std, size) Gaussian samples; rng.permutation(n) and rng.choice shuffle and sample. A second generator with seed 42 starts the same stream again. For independent parallel streams, rng.spawn(k) returns child generators whose streams do not overlap.
The older interface, np.random.seed(0) followed by np.random.rand(...), keeps one hidden global state that any library you call can advance, so the numbers your code gets depend on what ran before it. It still works and is kept for old code; new code passes a generator explicitly. NumPy fixes the bit stream for a seed but not the output of every distribution method across versions (NEP 19), so exact numbers are reproducible for one version, which is why the Code sections of this site print their versions. Most Code sections of the Machine Learning course create such a generator in their first lines, rng = np.random.default_rng(0), and draw every random number from it.
What changed#
| List of floats (the baseline) | NumPy array | |
|---|---|---|
| Storage | an array of pointers to separate float objects | one block of raw values, all of one dtype |
| Memory for 1000 doubles | 8 056 + 24 000 bytes | 8 000 bytes |
| Slicing | copies | a view: new strides over the same block |
| Integers | exact, unbounded | fixed width, wrap around silently |
| Elementwise arithmetic | a Python loop, about 20 ns per element here | one ufunc call, under 1 ns per element on large arrays here |
| Shapes | nested lists of any shape | rectangular, axes; broadcasting combines shapes |
| Matrix product | written by hand | @, handed to BLAS |
Limitations#
- Rectangular and homogeneous. An array holds one dtype in a rectangular shape. Ragged data (sentences of different lengths) must be padded or kept in lists; mixed columns (names next to numbers) belong in a pandas table, whose columns are arrays of different dtypes.
- Temporaries cost memory. Every intermediate of an expression is a full array.
((x - m) ** 2).sum()on a 1 GBxallocates two more gigabytes. In-place forms (x -= m),out=arguments and fused functions (x @ x,np.einsum) avoid some of them. - Sequential dependencies do not vectorize. A recurrence where each step needs the previous result, such as a simulation or a running state, stays a loop. Libraries that compile Python loops (Numba) or array libraries with compilers (JAX, PyTorch) handle these; they are not covered here.
- One machine, one build. The timings are from an Apple M1 with NumPy 2.5.4 and Accelerate as the BLAS. The memory model, the rules and the error messages are NumPy’s and hold everywhere; the numbers do not.
What you should remember#
- An array is a block of memory plus a dtype, a shape and strides. The element is at ; C order makes the last axis contiguous.
- Basic slicing,
.Tand mostreshapecalls return views that share memory; fancy and boolean indexing return copies.np.shares_memorytells you. Writing through a view changes the original. - NumPy integers have fixed width and wrap around silently.
float32has about 7 significant digits; accumulate long sums infloat64. - A ufunc runs one typed C loop over the whole array. It wins on large arrays and loses on a handful of elements; never loop over an array element by element in Python.
- Broadcasting lines shapes up on the right; sizes must be equal or 1, and size 1 is stretched with stride 0. It is silent when shapes happen to fit, so check shapes.
- Reductions with
axis=kremove axis ;keepdims=Truekeeps it for broadcasting back.@is the matrix product,*is elementwise. - Draw random numbers from one seeded generator,
np.random.default_rng(seed).
Code#
The script checks the note’s claims in order: the strides of C and Fortran order and the address formula; views and copies; integer wraparound and float32 accumulation; broadcasting and its silent shape bug; indexing and reductions; the matrix product; seeded generators; and finally a timing of the list loop against the vectorized forms. Run it with python -I arrays.py; it needs NumPy and takes a few seconds. The timings differ on every machine and from run to run; the other lines are exact for NumPy 2.5.4.
"""NumPy arrays: memory layout, views, dtypes, broadcasting and speed (Python 3.14, NumPy 2.x)."""
import timeit
import numpy as np
print("numpy", np.__version__)
# 1. An array is a block of memory + dtype + shape + strides
a = np.arange(12).reshape(3, 4)
f = np.asfortranarray(a)
print(a.dtype, a.shape, a.strides, f.strides)
i, j = 2, 1
print("offset of a[2, 1]:", i * a.strides[0] + j * a.strides[1], "C;", i * f.strides[0] + j * f.strides[1], "F")
# 2. Views share memory; fancy and boolean indexing copy
views = {"a[:, 1]": a[:, 1], "a.T": a.T, "a.reshape(4, 3)": a.reshape(4, 3), "a.T.reshape(12)": a.T.reshape(12),
"a[[0, 2]]": a[[0, 2]], "a[a > 5]": a[a > 5]}
print({name: np.shares_memory(a, v) for name, v in views.items()})
b = a.copy()
b[1][0] = 100 # b[1] is a view, so this writes into b
print(b[1])
# 3. Fixed-width integers wrap; float32 loses digits in long sequential sums
x8 = np.array([100, 120, 127], dtype=np.int8)
print(x8 + 1, np.array([200], dtype=np.uint8) + np.array([100], dtype=np.uint8))
v = np.full(10_000_000, 0.1, dtype=np.float32)
print(v.sum(), np.cumsum(v)[-1], v.sum(dtype=np.float64))
# 4. Broadcasting: (3, 1) with (4,) -> (3, 4); and the silent (n, 1) - (n,) bug
col, row = np.array([[0], [10], [20]]), np.array([1, 2, 3, 4])
print((col + row).shape, (col + row)[2].tolist(), np.broadcast_to(row, (3, 4)).strides)
y = np.array([1.0, 2.0, 3.0])
pred = y[:, None] # perfect predictions, but shape (3, 1)
print("wrong mse", ((y - pred) ** 2).mean(), "right mse", ((y - pred[:, 0]) ** 2).mean())
# 5. Reductions along an axis, and centering the columns of a matrix
X = np.array([[1.0, 2.0], [3.0, 4.0], [5.0, 9.0]])
print(X.mean(axis=0), (X - X.mean(axis=0)).mean(axis=0), X.sum(axis=1, keepdims=True).shape)
# 6. The matrix product
A = np.array([[1, 2], [3, 4]])
print(A @ np.array([1, 1]), (np.ones((5, 3, 4)) @ np.ones((4, 2))).shape)
# 7. A seeded generator gives the same stream every time
print(np.random.default_rng(42).random(3).round(4), np.random.default_rng(42).random(3).round(4))
# 8. A Python loop against vectorized code, sum of squares of n random floats
def loop(xs):
total = 0.0
for t in xs:
total += t * t
return total
rng = np.random.default_rng(0)
for n in (10, 1_000, 1_000_000):
x = rng.random(n)
xs = x.tolist()
number = max(1, 100_000 // n)
for name, fn in [("list loop", lambda: loop(xs)), ("(x*x).sum()", lambda: (x * x).sum()), ("x @ x", lambda: x @ x)]:
best = min(timeit.repeat(fn, number=number, repeat=5)) / number
print(f"n={n:>9,} {name:12} {best * 1e6:10.2f} us {best * 1e9 / n:7.2f} ns/element")
# Output with CPython 3.14.7 and NumPy 2.5.4 on an Apple M1 (macOS); the timings vary between machines and runs:
# numpy 2.5.4
# int64 (3, 4) (32, 8) (8, 24)
# offset of a[2, 1]: 72 C; 40 F
# {'a[:, 1]': True, 'a.T': True, 'a.reshape(4, 3)': True, 'a.T.reshape(12)': False, 'a[[0, 2]]': False, 'a[a > 5]': False}
# [100 5 6 7]
# [ 101 121 -128] [44]
# 1.0000001e+06 1.087937e+06 1000000.0149011612
# (3, 4) [21, 22, 23, 24] (0, 8)
# wrong mse 1.3333333333333333 right mse 0.0
# [3. 5.] [0. 0.] (3, 1)
# [3 7] (5, 3, 2)
# [0.774 0.4389 0.8586] [0.774 0.4389 0.8586]
# n= 10 list loop 0.25 us 25.00 ns/element
# n= 10 (x*x).sum() 0.58 us 57.54 ns/element
# n= 10 x @ x 0.50 us 50.14 ns/element
# n= 1,000 list loop 18.51 us 18.51 ns/element
# n= 1,000 (x*x).sum() 0.96 us 0.96 ns/element
# n= 1,000 x @ x 0.67 us 0.67 ns/element
# n=1,000,000 list loop 19735.83 us 19.74 ns/element
# n=1,000,000 (x*x).sum() 420.50 us 0.42 ns/element
# n=1,000,000 x @ x 177.75 us 0.18 ns/element
Next#
An array holds numbers of one type, addressed by position. Real data come as tables: a column of city names next to a column of temperatures and a column of dates, rows that are identified by a key rather than by their position, and values that are missing. The next note builds such a table, the pandas DataFrame, as labeled columns over arrays like these, and shows how to filter, group, join and reshape it.
References#
- NumPy documentation (2.x): The N-dimensional array (memory layout and strides); Copies and views; Data types (overflow); Universal functions; Broadcasting; Indexing on ndarrays;
numpy.matmul; Random sampling; CPU/SIMD optimizations. - S. Berg, M. Kühne, R. Gommers et al. NEP 50 — Promotion rules for Python scalars. NumPy 2.0, 2024.
- R. Kern. NEP 19 — Random number generator policy. 2018.
- C. R. Harris, K. J. Millman, S. J. van der Walt et al. Array programming with NumPy. Nature 585, 2020.
- S. van der Walt, S. C. Colbert, G. Varoquaux. The NumPy array: a structure for efficient numerical computation. Computing in Science & Engineering 13(2), 2011.
- J. VanderPlas. Python Data Science Handbook, 2nd ed. O’Reilly, 2023. Ch. 2 (introduction to NumPy).
- W. McKinney. Python for Data Analysis, 3rd ed. O’Reilly, 2022. Ch. 4 (NumPy basics).
- L. Ramalho. Fluent Python, 2nd ed. O’Reilly, 2022.