Sub topic 3
Choosing a direction with axis
This chapter assumes you can create arrays with np.array and np.arange, and that you can read an array's shape. It builds on the idea that a reduction turns many values into fewer values.
What axis selects
Every NumPy array dimension has a number, called an axis. A two-dimensional array with shape (2, 3) has two axes:
- Axis 0 runs down the rows. It has length 2.
- Axis 1 runs across the columns. It has length 3.
The axis argument names the axis that gets collapsed. NumPy combines all values along that axis into one value. The other axes stay.
import numpy as np
a = np.array([[1, 2, 3],
[4, 5, 6]])
print(a.sum()) # 21
print(a.sum(axis=0)) # [5 7 9]
print(a.sum(axis=1)) # [ 6 15]With no axis, NumPy reduces every axis and returns one number. With axis=0, it collapses the rows, so you get one total per column. With axis=1, it collapses the columns, so you get one total per row.
A reliable way to think about it
Many people read axis=0 as "sum the rows" and then expect one total per row. That reading is backwards. Use this rule instead: the axis you name disappears from the shape.
For shape (2, 3):
axis=0removes the2, leaving(3,).axis=1removes the3, leaving(2,).
You can check the rule in code before trusting a result.
import numpy as np
a = np.array([[1, 2, 3],
[4, 5, 6]])
print(a.sum(axis=0).shape) # (3,)
print(a.sum(axis=1).shape) # (2,)The same rule for other reductions
The axis argument works the same way for max, min, mean, and the other reductions.
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]a.max(axis=0) compares the values in each column and keeps the largest. a.min(axis=1) compares the values in each row and keeps the smallest.
The functions also exist in a second form, np.sum(a, axis=0). Both forms give the same result.
Negative axes
Axis numbers can be negative. They count from the last axis, so -1 is the last axis. For a two-dimensional array, axis=-1 equals axis=1, and axis=-2 equals axis=0. Negative numbers are useful when the same code must handle arrays with different numbers of dimensions, because -1 always means "the last axis".
Common errors
- Axis out of range. Asking for an axis the array does not have raises an error. For a two-dimensional array,
a.sum(axis=2)fails withAxisError: axis 2 is out of bounds for array of dimension 2. Compare the axis number witha.ndim. - Confusing the removed axis with the kept axis. If your result has the wrong length, print
.shapeand check which axis disappeared. - Expecting a column vector. The result of
a.sum(axis=1)has shape(2,), not(2, 1). A later chapter coverskeepdims, which keeps the reduced axis.
Exercise
An array m has shape (4, 5). What shape do m.sum(axis=0), m.sum(axis=1), and m.sum(axis=-1) return, and what does each total represent?
Show answer
m.sum(axis=0) has shape (5,), with one total per column. m.sum(axis=1) has shape (4,), with one total per row. m.sum(axis=-1) is the same as axis=1, so it also has shape (4,), with one total per row.
Exercise
Given a = np.array([[1, 2, 3], [4, 5, 6]]), which call returns [1 4], and why?
Show answer
a.min(axis=1). Axis 1 is collapsed, so the minimum is taken within each row. The smallest value in the first row is 1 and in the second row is 4.