NumPy Aggregations

Difficulty: Beginner

Aggregation functions are the workhorses of data analysis in NumPy. They reduce arrays to summary statistics, converting a large collection of numbers into meaningful insights. The most commonly used aggregations are np.sum() for total, np.mean() for average, np.std() for standard deviation, np.min() and np.max() for extremes, and np.argmin() and np.argmax() for the indices of extreme values. Each of these can operate on the entire array or along a specific axis.

np.sum() and np.mean() are perhaps the most frequently used aggregations. np.sum(arr) adds all elements in the array, while np.mean(arr) computes the arithmetic average. For a 2D array, specifying axis=0 computes column-wise sums or means (one value per column), and axis=1 computes row-wise sums or means (one value per row). The np.median() function computes the middle value of a sorted array, which is more robust to outliers than the mean.

np.std() computes the standard deviation, a measure of how spread out the values are from the mean. By default, NumPy computes the population standard deviation (dividing by N). If you need the sample standard deviation (dividing by N-1), pass ddof=1: np.std(arr, ddof=1). The related function np.var() computes the variance, which is the square of the standard deviation. These functions are essential for statistical analysis and data normalization.

np.argmin() and np.argmax() return the index of the minimum and maximum values respectively, rather than the values themselves. This is incredibly useful when you need to find where an extreme value occurs. For a 1D array, they return a single integer index. For multi-dimensional arrays without an axis parameter, they return the index into the flattened array. With an axis parameter, they return an array of indices along that axis. You can use np.unravel_index() to convert a flat index back into a multi-dimensional index.

The cumulative aggregation functions np.cumsum() and np.cumprod() compute running totals and running products respectively. Unlike sum() which returns a single value, cumsum() returns an array of the same shape where each element is the sum of all preceding elements including itself. For example, cumsum([1, 2, 3, 4]) gives [1, 3, 6, 10]. These are useful for computing cumulative distributions, running averages, and financial metrics like cumulative returns.

Code examples

Basic aggregation functions

import numpy as np

arr = np.array([12, 5, 8, 21, 3, 15])

print("Sum:", np.sum(arr))
print("Mean:", np.mean(arr))
print("Std:", round(np.std(arr), 4))
print("Min:", np.min(arr))
print("Max:", np.max(arr))
print("Median:", np.median(arr))

These functions reduce the entire array to a single summary value. The standard deviation is rounded for readability. The median (10.0) is the average of the two middle values (8 and 12) in the sorted array [3, 5, 8, 12, 15, 21].

argmin and argmax

import numpy as np

scores = np.array([78, 92, 65, 88, 95, 71])
students = ["Alice", "Bob", "Charlie", "Diana", "Eve", "Frank"]

best_idx = np.argmax(scores)
worst_idx = np.argmin(scores)

print("Highest score:", scores[best_idx], "by", students[best_idx])
print("Lowest score:", scores[worst_idx], "by", students[worst_idx])
print("Best index:", best_idx)
print("Worst index:", worst_idx)

argmax returns index 4 (Eve's score of 95) and argmin returns index 2 (Charlie's score of 65). These index values can be used to look up corresponding data in parallel arrays or lists.

Aggregations along axes in 2D

import numpy as np

data = np.array([[10, 20, 30],
                 [40, 50, 60],
                 [70, 80, 90]])

print("Column means (axis=0):", np.mean(data, axis=0))
print("Row means (axis=1):", np.mean(data, axis=1))
print("Column max (axis=0):", np.max(data, axis=0))
print("Row min (axis=1):", np.min(data, axis=1))
print("Argmax axis=0:", np.argmax(data, axis=0))
print("Argmax axis=1:", np.argmax(data, axis=1))

Column means (axis=0) are [40, 50, 60] because each column averages to the middle value. Row means (axis=1) are [20, 50, 80]. Argmax along axis=0 returns [2, 2, 2] because row 2 has the maximum in every column.

Cumulative sum and product

import numpy as np

arr = np.array([1, 2, 3, 4, 5])

print("Cumulative sum:", np.cumsum(arr))
print("Cumulative product:", np.cumprod(arr))

# 2D cumsum along axis
matrix = np.array([[1, 2, 3],
                   [4, 5, 6]])
print("Cumsum axis=0:")
print(np.cumsum(matrix, axis=0))
print("Cumsum axis=1:")
print(np.cumsum(matrix, axis=1))

Cumulative sum builds a running total: [1, 1+2, 1+2+3, ...]. Cumulative product builds a running product: [1, 1*2, 1*2*3, ...] which gives factorials here. Along axis=0, each row accumulates from above; along axis=1, each column accumulates from the left.

Key points

Concepts covered

sum, mean, std, min, max, argmin, argmax, axis parameter