Last updated
Aggregations
Definition: Aggregation functions summarise a whole array into a single number — sum, mean, min, max, standard deviation, and more.
Example 1 — summary statistics
import numpy as np
a = np.array([4, 8, 15, 16, 23, 42])
print("sum: ", a.sum())
print("mean:", a.mean())
print("min: ", a.min())
print("max: ", a.max())
print("std: ", round(a.std(), 2))
Example 2 — along an axis
For 2D arrays you can summarise by column (axis=0) or by row (axis=1):
import numpy as np
b = np.array([[1, 2, 3],
[4, 5, 6]])
print("column sums:", b.sum(axis=0)) # [5 7 9]
print("row sums: ", b.sum(axis=1)) # [6 15]
The axis idea
axis=0— collapse the rows, one result per columnaxis=1— collapse the columns, one result per row
💡 Tip: "axis 0 goes down, axis 1 goes across" is a helpful way to remember it.
Try it Yourself
Output
Ad · responsive