Aggregation Functions
What You'll Learn
You'll learn how to compute summary statistics (sum, mean, standard deviation, min, max) across NumPy arrays using built-in functions—essential for understanding dataset properties before building ML models.
The Core Idea
When working with machine learning data, you rarely care about individual numbers—you need to understand the big picture. Aggregation functions collapse arrays of numbers into single meaningful values that describe your data's characteristics.
Think of it like getting a weather report: instead of knowing the temperature at every second of the day, you want the average temperature, the hottest point, and how much it varied. That's aggregation.
Key Aggregation Functions
NumPy provides these fundamental aggregations:
np.sum()- Total of all valuesnp.mean()- Average valuenp.std()- Standard deviation (spread)np.min()- Smallest valuenp.max()- Largest value
The Power of Axes
Here's where it gets powerful: you can aggregate along specific axes (dimensions) of your array.
import numpy as np
# Student test scores: 3 students, 4 subjects
scores = np.array([[85, 90, 78, 92],
[88, 85, 91, 87],
[76, 82, 85, 89]])
# Average across all scores
overall_avg = np.mean(scores) # Single number
# Average per student (across subjects, axis=1)
student_avgs = np.mean(scores, axis=1) # [86.25, 87.75, 83.0]
# Average per subject (across students, axis=0)
subject_avgs = np.mean(scores, axis=0) # [83.0, 85.67, 84.67, 89.33]
Axis 0 goes down rows (across students), axis 1 goes across columns (across subjects).
Why This Matters for ML
Before training any model, you must understand your features: their typical values (mean), variability (std), and ranges (min, max). These aggregations form the foundation of feature analysis and data preprocessing.
Key Takeaway: Aggregation functions compress arrays into meaningful statistics, and controlling the axis parameter lets you compute summaries across specific dimensions—critical for analyzing ML datasets feature-by-feature.