Aggregations
Aggregations summarize your data. Think of it like taking a survey and finding the average response. NumPy has built-in functions for all common statistics.
Basic Aggregations
sum, mean, std, min, max - the essentials.
import numpy as np
arr = np.array([1, 2, 3, 4, 5, 6])
print(f"Sum: {np.sum(arr)}")
print(f"Mean: {np.mean(arr)}")
print(f"Std: {np.std(arr):.2f}")
print(f"Min: {np.min(arr)}")
print(f"Max: {np.max(arr)}")
These functions work on the entire array. But what if you want to aggregate along specific dimensions?
The Axis Parameter
axis=0 operates along rows, axis=1 operates along columns.
import numpy as np
matrix = np.array([[1, 2, 3], [4, 5, 6]])
print(f"Sum axis=0 (rows): {np.sum(matrix, axis=0)}")
print(f"Sum axis=1 (cols): {np.sum(matrix, axis=1)}")
Here is the thing - axis=0 collapses rows (gives column sums), axis=1 collapses columns (gives row sums).
Argmin & Argmax
Find the index of min/max values.
import numpy as np
arr = np.array([3, 1, 4, 1, 5, 9])
print(f"Index of min: {np.argmin(arr)}")
print(f"Index of max: {np.argmax(arr)}")
One thing that confused me at first was the axis parameter. Remember: axis=0 is "down" (rows), axis=1 is "across" (columns).
Try it Yourself →Key Takeaways
- sum, mean, std, min, max are basic aggregations
- axis=0 operates along rows, axis=1 along columns
- argmin/argmax return indices of min/max values
- Aggregations reduce array dimensions