NumPy for Numerical Computing · Lesson 109

Aggregation Functions

NumPy aggregation functions reduce many array values to a summary such as sum, mean, minimum, maximum or standard deviation.

ConceptWorked examplePracticeKnowledge check
Textbook walkthrough

Aggregation Functions

NumPy aggregation functions reduce many array values to a summary such as sum, mean, minimum, maximum or standard deviation. The axis argument controls which dimension is reduced: without an axis the whole array is summarised; with axis=0 or axis=1 a two-dimensional array produces column-wise or row-wise summaries.

Learning goal: explain why Aggregation Functions behaves this way, apply it to a small example, and verify the result independently. Begin by being able to justify this first step: Create an array and inspect its shape.

Deeper walkthrough

Read Aggregation Functions as a mechanism, not a recipe

Treat this as a sequence of observable decisions rather than one opaque command. Stage 1: Create an array and inspect its shape. Stage 2: Choose the statistic that answers the question. Stage 3: Choose the axis that corresponds to the groups you intend to summarise. Final checkpoint: Check the output shape and one manually computable row/column.

Mechanism

Follow the transformation

Create an array and inspect its shape.

Choose the statistic that answers the question.

Choose the axis that corresponds to the groups you intend to summarise.

Evidence

Know what would convince you

  • Trace a tiny input by hand and compare the runtime result.
  • Inspect type, value/shape and any mutation/side effect explicitly.
Useful distinctionInput: Objects/values supplied to the operation.
Visual demonstration of Aggregation Functions
Visual demonstration: use the diagram to trace the main objects and state changes involved in Aggregation Functions.
Click a stage to inspect what happens, what changes, and what should be checked before moving on.
Stage 1

Create an array and inspect its…

Create an array and inspect its shape. For Aggregation Functions, make this checkpoint explicit by recording the evidence inspected, the expected result, and the condition that would make you reject the current result.

Verification focus: record the evidence you inspected and the condition that would make this stage fail.
How it works

Trace the mechanism step by step

  1. Create an array and inspect its shape.
  2. Choose the statistic that answers the question.
  3. Choose the axis that corresponds to the groups you intend to summarise.
  4. Use NaN-aware variants such as nanmean only when ignoring NaN is a deliberate rule.
  5. Check the output shape and one manually computable row/column.
Worked demonstration

Reduce by axis

# Step 1 — Import the module so its functions/classes are available to the rest of this example.
import numpy as np
# Step 2 — Construct `x` as an array so vectorised numerical operations can be applied consistently.
x = np.array([[1, 2, 3], [4, 5, 6]])
# Step 3 — Display the current value explicitly so the result/state can be inspected during execution.
print(x.mean())
# Step 4 — Display the current value explicitly so the result/state can be inspected during execution.
print(x.mean(axis=0))
# Step 5 — Display the current value explicitly so the result/state can be inspected during execution.
print(x.sum(axis=1))
Expected / illustrative result
Overall mean is 3.5; column means are [2.5, 3.5, 4.5]; row sums are [6, 15].
Interpret the result.

For Aggregation Functions, trace representative input values into the result and verify shape, dtype, row grain, axis or key behaviour that the operation can change.

Distinctions & related ideas

Place the concept correctly

InputObjects/values supplied to the operation.
StateNames or mutable objects that may change during execution.
OutputReturned value, side effect, file, plot or exception to inspect.
Use deliberately

When it is appropriate

Use Aggregation Functions when it answers a defined question in NumPy for Numerical Computing and its inputs/assumptions match the current data or program state.

Boundary conditions

When to stop or reconsider

Reconsider Aggregation Functions when the required information is unavailable, the operation would violate a validation/data boundary, or a simpler operation answers the question more transparently.

Common mistakes

Failure modes to recognise

  • Running the operation on the wrong object/type or in the wrong environment.
  • Inferring correctness from “no exception” without checking the produced value/state.
  • Hiding a boundary case instead of making its behaviour explicit.
Verification

How to check the result

  • Trace a tiny input by hand and compare the runtime result.
  • Inspect type, value/shape and any mutation/side effect explicitly.
  • Run an edge or invalid case and confirm the exception/behaviour is deliberate.
Hands-on practice

Demonstrate understanding

Try this:

Construct a tiny example of Aggregation Functions. First create an array and inspect its shape. Then choose the statistic that answers the question. Predict the result before execution and explain one boundary or failure case.

Use a 2×3 array. Keep shape, dtype and axis visible, and verify one element or reduction by hand.
Knowledge check

Check reasoning, not memorisation

Which approach best demonstrates understanding of Aggregation Functions?

Quick reference

Remember the logic

Step 1Create an array and inspect its shape.
Step 2Choose the statistic that answers the question.
Step 3Choose the axis that corresponds to the groups you intend to summarise.
Step 4Use NaN-aware variants such as nanmean only when ignoring NaN is a deliberate rule.
Lesson summary

What to remember

  • NumPy aggregation functions reduce many array values to a summary such as sum, mean, minimum, maximum or standard deviation. The axis argument controls which dimension is reduced: without an axis the whole array is summarised; with axis=0 or axis=1 a two-dimensional array produces column-wise or row-wise summaries.
  • Create an array and inspect its shape.
  • Running the operation on the wrong object/type or in the wrong environment.
  • Trace a tiny input by hand and compare the runtime result.