Summarize Along an Axis

Compute totals, means, minima, and maxima while naming the axis being combined and predicting the result shape.

An array can hold several related measurements, but a summary must still say which measurements it combines. In a two-dimensional array, the axis argument makes that choice explicit.

We will use the same table throughout this lesson:

Its shape is (3, 2). Axis 0 contains three observations. Axis 1 contains two sensors. Naming the axes before calculating helps us predict what a summary should mean and what shape it should have.

Reduce One Axis

A reduction combines several values into fewer values. NumPy provides familiar reductions such as sum, mean, min, and max:

Here axis=0 says to combine values along the observation axis. That axis is removed from the result. The sensor axis remains, so every result has shape (2,): one value for each sensor.

[59. 66.]
[19.66666667 22.        ]
[18.5 20. ]
[21.5 24. ]

The first sensor mean can be checked without NumPy:

(18.5 + 21.5 + 19.0) / 3
= 59.0 / 3
= 19.666666...

The second sensor mean is (20.0 + 22.0 + 24.0) / 3 = 22.0. The two hand calculations agree with the two entries returned by readings.mean(axis=0).

Q1. Predict a per-sensor summary

Before running the expression, predict the value and shape of readings.max(axis=0).

Choose one

Select one choice, then check.

HintName the axis that disappears

axis=0 combines the three observations. The two-position sensor axis remains.

SolutionKeep one maximum per sensor

Sensor 0 has maximum 21.5, and sensor 1 has maximum 24.0. The result is [21.5, 24.0] with shape (2,).

Not attempted
Review

Not marked done.

Change the Axis, Change the Question

Using axis=1 combines the two sensors within each observation:

[19.25 21.75 21.5 ]
(3,)

Axis 1 disappears, while the three-position observation axis remains. The result therefore contains one mean per observation, not one mean per sensor.

ExpressionAxis combinedResult meaningResult shape
readings.mean(axis=0)observationsone mean per sensor(2,)
readings.mean(axis=1)sensorsone mean per observation(3,)
readings.mean()all entriesone mean for the entire table()

The same axis rule applies to sum, min, and max. Start from the question, name the axis being combined, and predict which axes will remain. The method name alone does not tell us what the returned values mean.

Q2. Choose the axis for each question

Match each expression to the question it answers.

Choose one

Select one choice, then check.

HintTrack what remains

After axis=0, the sensor axis remains. After axis=1, the observation axis remains.

SolutionRead the remaining axis

min(axis=0) combines the rows and returns one minimum per sensor. sum(axis=1) combines the columns and returns one total per observation.

Not attempted
Review

Not marked done.

Preserve an Axis Only When Its Position Matters

By default, a reduced axis disappears. Suppose we want to display the two sensor means as a summary row beneath the two sensor columns. Keeping a row axis makes that alignment visible. keepdims=True preserves the reduced axis with length 1:

[[19.66666667 22.        ]]
(1, 2)

The result still has two sensor positions, but its observation axis now has length 1. It can sit beneath the table as one summary row with the same two columns. Without that display or alignment need, the simpler (2,) result is easier to read. keepdims does not change the calculated numbers; it changes only the result shape.

Q3. Compute and preserve the intended shape

Complete the program so it prints one mean per sensor with shape (2,), then the same two means as a row with shape (1, 2).

Editable Python

Command/Ctrl + Enter. Python runs in your browser.

Ready to run.

HintCombine observations twice

Use axis=0 for both means. Set keepdims=True only for row.

SolutionPreserve the reduced axis in the second result
Not attempted
Review

Not marked done.

An axis reduction combines the named axis and normally removes it. Predict the remaining axes before running the code, and verify an important result by hand. Preserve a reduced axis only when its position makes a later alignment easier to see.

Pause and reflect

In your own words, note what you understood, what remains unclear, or what you want to revisit. The note stays with this lesson.

0 of 3 exercises marked done

Review

Not marked done.