Sub topic 5
Predicting the Shape of the Result
This chapter assumes you can create arrays with np.array and np.arange, read an array's shape, and know that axis=0 runs down the rows while axis=1 runs across the columns. Here you learn one rule that lets you state the shape of any aggregation result before you run the code.
The rule: the reduced axis disappears
An aggregation such as sum, mean, min, or max collapses one axis into a single value per position. The collapsed axis is removed from the shape. Every other axis keeps its length and its order.
import numpy as np
a = np.array([[1, 2, 3],
[4, 5, 6]])
print(a.shape) # (2, 3)
print(a.sum(axis=0).shape) # (3,)
print(a.sum(axis=1).shape) # (2,)
print(a.sum().shape) # ()Read the shape (2, 3) as "axis 0 has length 2, axis 1 has length 3".
axis=0removes the 2, so the result has shape(3,). You get one value per column.axis=1removes the 3, so the result has shape(2,). You get one value per row.- With no axis, every axis is removed. The result is a single scalar with shape
().
Why the result is one-dimensional
The result of a.sum(axis=1) is [ 6 15]. It is a one-dimensional array of length 2. It is not a column with shape (2, 1), even though it holds one total per row. This matters later, because a one-dimensional result lines up with the last axis of the original array when you broadcast.
Predicting with a procedure
Use these steps for any array and any axis.
- Write down the shape as a tuple.
- Delete the entries for the axes you reduce.
- What remains is the result shape.
The same steps work for max, min, and mean.
import numpy as np
a = np.array([[1, 2, 3],
[4, 5, 6]])
print(a.max(axis=0)) # [4 5 6]
print(a.min(axis=1)) # [1 4]
print(a.mean(axis=0)) # [2.5 3.5 4.5]
print(a.sum(axis=1).shape) # (2,)The values of max, min, and mean differ from sum, but each result has the same shape as the matching sum call. The shape depends on the axis you pick, not on the function.
Three dimensions
The rule does not change when the array has more axes. Delete the reduced entries and keep the rest.
import numpy as np
b = np.arange(24).reshape(2, 3, 4)
print(b.sum(axis=0).shape) # (3, 4)
print(b.sum(axis=2).shape) # (2, 3)
print(b.sum(axis=(0, 2)).shape) # (3,)For shape (2, 3, 4), axis=0 leaves (3, 4) and axis=2 leaves (2, 3). A tuple of axes removes all of them at once, so axis=(0, 2) leaves only the middle length, (3,).
Negative axes
A negative axis counts from the end. axis=-1 always means the last axis, which is useful when you don't know how many dimensions the array has.
import numpy as np
b = np.arange(24).reshape(2, 3, 4)
print(b.sum(axis=-1).shape) # (2, 3)Common errors
- Expecting
(2, 1)instead of(2,). Withoutkeepdims=True, the reduced axis is gone. The next chapter covers how to keep it. - Using an axis that does not exist. For a two-dimensional array,
axis=2raisesAxisError, because only axes 0 and 1 exist. - Mixing up rows and columns. If you remember "the named axis disappears", you can check the result length against the shape tuple. For shape
(2, 3), a result of length 3 must come fromaxis=0.
Exercise
An array x has shape (4, 5, 6). What are the shapes of x.sum(axis=1), x.mean(axis=(0, 1)), and x.max(axis=-1)?
Show answer
x.sum(axis=1) removes the 5, giving (4, 6). x.mean(axis=(0, 1)) removes the 4 and the 5, giving (6,). x.max(axis=-1) removes the last axis, the 6, giving (4, 5).
Exercise
For a = np.array([[1, 2, 3], [4, 5, 6]]), the call a.sum(axis=1) returns [ 6 15]. What is its shape, and why is it not (2, 1)?
Show answer
The shape is (2,). The reduced axis (length 3) is removed from the shape instead of being kept as length 1. Only keepdims=True leaves it in place as length 1, which would give (2, 1).