Aggregation functions collapse values to statistics: sum, mean, min, max, std, var, argmin, argmax, and percentile.
Method vs function
Both work: arr.mean() and np.mean(arr). Methods are convenient; functions accept axis, dtype, and out parameters uniformly.
Multi-axis aggregation
import numpy as np
a = np.arange(24).reshape(2, 3, 4)
print(a.mean(axis=(1, 2))) # one mean per outer slice
NaN-aware variants
np.nanmean, np.nansum ignore NaN—essential for real-world missing sensor readings (see NaN lesson).
Important interview questions and answers
- Q: argmax?
A: Index of maximum element—useful for classification predicted class. - Q: std vs var?
A: Variance is squared deviation; std is square root of variance (same units as data).
Self-check
- Find index of minimum value in 1D array.
- Which function ignores NaN in mean?
Tip: Use nanmean when missing values appear in real data.
Interview prep
- argmax?
Index of maximum—classification predicted class index.
- nanmean?
Mean skipping NaN values.