Aggregation Functions in NumPy
NumPy provides various aggregation functions to compute summary statistics on arrays efficiently. These functions help perform operations such as sum, mean, minimum, maximum, and more.
1. Sum of Elements (sum)
The sum() function computes the total sum of all elements in an array.
import numpy as np arr = np.array([[1, 2, 3], [4, 5, 6]]) total_sum = np.sum(arr) print(total_sum)
Output:
21
2. Mean (Average) of Elements (mean)
The mean() function computes the arithmetic mean of the array elements.
avg = np.mean(arr) print(avg)
Output:
3.5
3. Minimum and Maximum Values (min, max)
The min() and max() functions return the minimum and maximum values in an array.
minimum = np.min(arr) maximum = np.max(arr) print(minimum, maximum)
Output:
1 6
4. Standard Deviation and Variance (std, var)
The std() function computes the standard deviation, while the var() function calculates variance.
std_dev = np.std(arr) variance = np.var(arr) print(std_dev, variance)
Output:
1.707825127659933 2.9166666666666665
5. Aggregation Along an Axis
Aggregation functions can be applied along a specific axis.
column_sum = np.sum(arr, axis=0) row_mean = np.mean(arr, axis=1) print(column_sum) print(row_mean)
Output:
[5 7 9] [2. 5.]
Conclusion
Aggregation functions in NumPy help in statistical analysis by computing sums, means, min/max values, standard deviations, and variances. They can be applied to entire arrays or specific axes for efficient computation.