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
a = np.array([3, 1, 4, 1, 5, 9])a.sum() # 23a.mean() # 3.83…a.min(), a.max()a.std() # standard deviationa.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):
m = np.array([[1, 2, 3],[4, 5, 6]])m.sum() # 21 everythingm.sum(axis=0) # [5 7 9] down each columnm.sum(axis=1) # [6 15] across each row
Key takeaways
- 01Aggregations (sum, mean, min, max, std, argmax) reduce an array to summary numbers.
- 02axis=0 collapses rows → one value per column; axis=1 collapses columns → one per row.
- 03The collapsed axis disappears from the result shape.
- 04arg-variants (argmax, argmin) return the position of the extreme value.
