Reductions along axes

Intermediate

Introduction

Data is usually summarised before it is understood: the total sales, the average mark per student, the hottest day of each month. NumPy's reductions do this in one call, for the whole array or along one axis. The axis argument is the part that confuses everyone at first, so this lesson spends most of its time on what axis=0 and axis=1 really mean.

How it works

Without an axis, a reduction combines every element:

m = np.array([[1, 2, 3], [4, 5, 6]])
print(m.sum())      # 21
print(m.mean())     # 3.5

With axis=k, the reduction runs along axis k, and that axis disappears from the shape. For a 2-D array, axis=0 runs down the rows, combining each column, and axis=1 runs along each row:

print(m.sum(axis=0))   # [5 7 9]     one value per column, shape (3,)
print(m.sum(axis=1))   # [ 6 15]     one value per row, shape (2,)
print(m.max(axis=0))   # [4 5 6]

A good way to remember: the axis you name is the one that is collapsed. Shape (2, 3) reduced along axis 0 becomes (3,); along axis 1, (2,).

argmin and argmax give positions instead of values. Without an axis, the position is in the flattened array: m.argmax() is 5, the last of six elements. With an axis, you get one position per row or column, and the first is chosen on ties.

Reducing removes a dimension, which can stop the result broadcasting back against the original. keepdims=True keeps the reduced axis with size 1:

print(m.sum(axis=1, keepdims=True))
# [[ 6]
#  [15]]
print(m / m.sum(axis=1, keepdims=True))   # each row as fractions of its total

np.cumsum is not a reduction but a running total: np.cumsum([1, 2, 3, 4]) is [ 1 3 6 10]. std and var give the spread, and np.nansum and np.nanmean skip nan values.

Example

# Reductions modelled on a 2 x 3 grid: whole-array, down the columns (axis 0) and along the rows (axis 1).
m = [[1, 2, 3], [4, 5, 6]]
flat = [x for row in m for x in row]

print("sum:", sum(flat))
print("sum axis 0:", [sum(col) for col in zip(*m)])  # m.sum(axis=0)
print("sum axis 1:", [sum(row) for row in m])  # m.sum(axis=1)
print("mean axis 1:", [sum(row) / len(row) for row in m])
print("max:", max(flat), "argmax:", flat.index(max(flat)))  # argmax is a position in the flattened array
print("sum axis 1, keepdims:", [[sum(row)] for row in m])  # shape (2, 1)

running, total = [], 0
for x in flat:
    total += x
    running.append(total)
print("cumsum:", running)
Open in compiler

Line by line

  1. The whole-array sum combines all six values.
  2. zip(*m) gives the columns, so summing them models axis=0; summing each row models axis=1.
  3. argmax is the position of the maximum in the flattened list, as in NumPy.
  4. Wrapping each row's sum in a list models keepdims=True: the result is still 2-D, shape (2, 1).
  5. The running total models cumsum.

Expected result

sum: 21
sum axis 0: [5, 7, 9]
sum axis 1: [6, 15]
mean axis 1: [2.0, 5.0]
max: 6 argmax: 5
sum axis 1, keepdims: [[6], [15]]
cumsum: [1, 3, 6, 10, 15, 21]

In the real world

Marks tables reduce along axis 1 for each student's total and along axis 0 for each subject's average. Images reduce along the colour axis to make greyscale. In machine learning, X.mean(axis=0) and X.std(axis=0) standardise each feature, and argmax(axis=1) turns a table of class probabilities into predicted labels.

Common mistakes

  • Mixing up axis 0 and 1: axis 0 collapses the rows, giving one result per column.
  • Dividing by a reduced array without keepdims and getting a broadcasting error or wrong pairing.
  • Using argmax as a value instead of a position.
  • Averaging data with nan, which gives nan; use np.nanmean.

Best practices

  • Say out loud which axis disappears before writing axis=.
  • Use keepdims=True when the result will be combined with the original array.
  • Check .shape of every reduction while learning.

Summary

Reductions such as sum, mean, min, max and std combine values: without an axis into one number, with axis=k along that axis, which disappears from the shape. argmin and argmax give positions, keepdims=True keeps a size-1 axis for broadcasting, and cumsum gives running totals.

Quick check

1. For m of shape (4, 5), what is the shape of m.sum(axis=0)?
2. m = np.array([[1, 9], [7, 3]]). What is m.argmax()?
3. Why use keepdims=True?
4. What is np.cumsum([2, 2, 2])?

Key takeaways

  • Without an axis a reduction gives one number; with axis=k it removes axis k.
  • axis=0 reduces down the columns, axis=1 along each row.
  • keepdims=True keeps the reduced axis as size 1, ready to broadcast.

Practice problems for this lesson

My notes

Write down what you want to remember from this lesson. Notes are saved on this device as you type, and appear on your dashboard.