Sub topic 10
Three dimensions and tuples of axes
This chapter assumes you can create arrays with np.arange and reshape, and that you know how to read a shape such as (2, 3, 4). It also assumes you know the 2D rule: axis=0 gives one result per column, and axis=1 gives one result per row.
Axes in a three-dimensional array
A 3D array has three axes, numbered 0, 1, and 2. For shape (2, 3, 4), axis 0 has length 2, axis 1 has length 3, and axis 2 has length 4. You can picture this as 2 blocks, each with 3 rows and 4 columns.
The rule from two dimensions carries over unchanged. The axis you name is collapsed, and its length disappears from the result's shape. The other axes stay.
import numpy as np
b = np.arange(24).reshape(2, 3, 4)
print(b.sum(axis=0).shape) # (3, 4)
print(b.sum(axis=1).shape) # (2, 4)
print(b.sum(axis=2).shape) # (2, 3)
print(b.sum(axis=0))
# [[12 14 16 18]
# [20 22 24 26]
# [28 30 32 34]]b.sum(axis=0) adds the two blocks together, element by element. Each output value is b[0, j, k] + b[1, j, k].
Reducing several axes at once
Pass a tuple to axis to collapse more than one axis in a single call. The result keeps only the axes you did not list.
import numpy as np
b = np.arange(24).reshape(2, 3, 4)
print(b.sum(axis=(0, 2)).shape) # (3,)
print(b.sum(axis=(0, 2))) # [ 60 92 124]
print(b.sum(axis=(1, 2))) # [ 66 210]
print(b.sum(axis=(0, 1, 2))) # 276axis=(0, 2) leaves axis 1. Each of the 3 results is the sum of every value that shares that axis-1 position, across both blocks and all columns. axis=(1, 2) gives one total per block, which is a handy way to get a per-sample summary from a batch of images or tables. Listing every axis is the same as passing no axis, so you get a single number.
| Call on shape `(2, 3, 4)` | Result shape |
|---|---|
| `sum(axis=0)` | `(3, 4)` |
| `sum(axis=(0, 1))` | `(4,)` |
| `sum(axis=(0, 2))` | `(3,)` |
| `sum(axis=(1, 2))` | `(2,)` |
| `sum(axis=None)` | `()` (a scalar) |
Negative axes
Negative numbers count from the last axis. In a 3D array, axis=-1 means axis 2, and axis=-2 means axis 1. Writing -1 is useful when the same code must work on arrays with different numbers of leading dimensions, because "the last axis" stays the last axis.
keepdims with tuples
keepdims=True also works with a tuple. Every reduced axis stays with length 1, so the result still broadcasts against the original array.
import numpy as np
b = np.arange(24).reshape(2, 3, 4)
totals = b.sum(axis=(0, 2), keepdims=True)
print(totals.shape) # (1, 3, 1)
shares = b / totals
print(shares.shape) # (2, 3, 4)
print(shares.sum(axis=(0, 2))) # [1. 1. 1.]Here every slice along axis 1 is divided by its own total, so the values in each slice sum to 1.
Common mistakes
- Passing a list instead of a tuple. Use
axis=(0, 2). A list such as[0, 2]is rejected with aTypeError. - Naming an axis that does not exist. On a 3D array,
b.sum(axis=3)raisesAxisError: axis 3 is out of bounds for array of dimension 3. - Guessing the result shape. Write the original shape, cross out the reduced axes, and read what is left.
Exercise
An array x has shape (5, 6, 7). What is the shape of x.mean(axis=(0, 1)), and what is the shape of x.mean(axis=(0, 1), keepdims=True)?
Show answer
The first result has shape (7,) because only axis 2 remains. With keepdims=True the shape is (1, 1, 7), because the reduced axes stay with length 1.