Skip to main content
Albero Academy

Aggregations & Axes

Reduce arrays to summary numbers, and control which axis you collapse.

Operations10 min

Aggregations boil an array down to summary numbers — totals, means, maxima. The one idea that trips up every beginner exactly once is the axis argument, which controls which dimension you collapse.

Whole-array reductions

summaries
a = np.array([3, 1, 4, 1, 5, 9])
a.sum() # 23
a.mean() # 3.83…
a.min(), a.max()
a.std() # standard deviation
a.argmax() # 5 (index of the maximum)

The axis argument

On a 2-D array, axis=0 collapses the rows (a result per column); axis=1 collapses the columns (a result per row):

axis=0 vs axis=1
m = np.array([[1, 2, 3],
[4, 5, 6]])
m.sum() # 21 everything
m.sum(axis=0) # [5 7 9] down each column
m.sum(axis=1) # [6 15] across each row

Key takeaways

  1. 01Aggregations (sum, mean, min, max, std, argmax) reduce an array to summary numbers.
  2. 02axis=0 collapses rows → one value per column; axis=1 collapses columns → one per row.
  3. 03The collapsed axis disappears from the result shape.
  4. 04arg-variants (argmax, argmin) return the position of the extreme value.

Finished this chapter?

Mark it complete to track your progress through NumPy.