0
0
NumPydata~5 mins

Why aggregation matters in NumPy

Choose your learning style9 modes available
Introduction

Aggregation helps us find simple summaries from lots of numbers. It makes data easier to understand by showing totals, averages, or counts.

You want to know the average score of a class from all students' scores.
You need the total sales from daily sales numbers to see overall performance.
You want to find the highest temperature recorded in a week.
You want to count how many times a certain event happened in your data.
Syntax
NumPy
numpy.sum(array)
numpy.mean(array)
numpy.max(array)
numpy.min(array)
numpy.count_nonzero(array)

Aggregation functions take an array and return a single number.

You can apply them to the whole array or along specific rows or columns.

Examples
This adds all numbers in the array: 1+2+3+4+5 = 15.
NumPy
import numpy as np
arr = np.array([1, 2, 3, 4, 5])
print(np.sum(arr))
This finds the average: (1+2+3+4+5)/5 = 3.0.
NumPy
print(np.mean(arr))
This finds the biggest number: 5.
NumPy
print(np.max(arr))
This counts how many numbers are greater than 3: 2 (which are 4 and 5).
NumPy
print(np.count_nonzero(arr > 3))
Sample Program

This program shows how aggregation helps summarize sales data. It finds the total sales, average sales per day, and the highest sales each store made on any day.

NumPy
import numpy as np

# Create a 2D array representing daily sales for 3 stores over 4 days
sales = np.array([
    [10, 15, 20, 25],
    [5, 10, 15, 20],
    [8, 12, 18, 22]
])

# Total sales for all stores and days
total_sales = np.sum(sales)

# Average sales per day (across all stores)
avg_sales_per_day = np.mean(sales, axis=0)

# Maximum sales in a single day for each store
max_sales_per_store = np.max(sales, axis=1)

print(f"Total sales: {total_sales}")
print(f"Average sales per day: {avg_sales_per_day}")
print(f"Max sales per store: {max_sales_per_store}")
OutputSuccess
Important Notes

Aggregation reduces many numbers into a few meaningful values.

Using axis=0 aggregates down columns; axis=1 aggregates across rows.

Aggregation is useful for quick insights and comparisons.

Summary

Aggregation turns many data points into simple summaries.

Common aggregations include sum, mean, max, min, and count.

They help us understand data quickly and clearly.