/Curriculum/Core NumPyStatistics & Reductions

17. Aggregate FunctionsBeginner

Summarize data with sum(), mean(), min(), max(), std(), var(), and ptp().

12 mins

Concept Overview

Aggregate functions reduce an entire array or dimension down into summary statistics like total sum, average mean, minimum, maximum, and standard deviation.

Hardware Mental Model

Aggregating is like squeezing a sponge of numbers: all individual droplets condense into one or two essential metrics like total volume (sum) or average flow (mean).

Key Concepts (1)Click snippet to load in editor

You can call either `arr.sum()` as a method or `np.sum(arr)` as a top-level function. Both produce identical results.

total = arr.sum(); avg = arr.mean()

⚠ Common Mistakes & Pitfalls

Avoid these frequent beginner syntax and logic traps
❌ Incorrect:sum(data) # Python built-in sum (slower on ndarrays)
✓ Correct:data.sum() # or np.sum(data)

Python's built-in `sum()` does not take advantage of NumPy's contiguous memory layout.

Pro Tip: Always use `arr.sum()` or `np.sum(arr)` for superior performance.

📌 Quick Revision

Core takeaway points from this topic
`sum()`, `mean()`, `prod()`: Basic totals and averages.
`min()`, `max()`: Extrema values.
`std()`, `var()`: Spread and dispersion statistics.
`ptp()`: Peak-to-peak range (`max - min`).
Editor: 17. Aggregate Functions
Python ● Ready
Initializing Python IDE...
Ctrl+Enter
Execution Result
Click "RUN CODE" to execute and inspect array state

🎯 Try It Yourself: Practice Challenge

Hands-on Mode

Solve the objective below using NumPy vectorized syntax

Objective:Calculate Array Mean

Create `arr = np.array([10, 20, 30, 40, 50])`, calculate its mean using `.mean()`, and print.

Expected Output Target:30.0
Practice Workspace
Python ● Ready
Initializing Python IDE...
Ctrl+Enter
Execution Result
Click "RUN CODE" to execute and inspect array state