Aggregation Functions in NumPy

NumPy provides various aggregation functions to compute summary statistics on arrays efficiently. These functions include sum, mean, median, standard deviation, variance, 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. Median of Elements (median)

The median() function computes the median value of the array elements.

    median_value = np.median(arr)
    print(median_value)
        

Output:

    3.5
        

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. 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
        

6. 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, medians, min/max values, standard deviations, and variances. They can be applied to entire arrays or specific axes for efficient computation.





Advertisement