Reductions Over Axes

A reduction summarizes values.

An axis-specific reduction summarizes along one direction and leaves the other directions.

Matrix reductions

A.shape is (2, 3).

  • A.sum(axis=0) leaves one value per column.
  • A.sum(axis=1) leaves one value per row.

Reduce by axis

Runs locally with Python in your browser.

Ready to run.

The removed-axis rule

When a reduction does not keep dimensions, the reduced axis disappears.

For shape (2, 3):

sum(axis=0) -> (3,)
sum(axis=1) -> (2,)

This rule becomes useful when arrays have more dimensions.

Mean over examples

If X.shape is (examples, features), then:

X.mean(axis=0)

computes one mean per feature.

X.mean(axis=1)

computes one mean per example.

Exercise: Feature means

If X.shape is (5, 3) and axis 0 means examples, what is the shape of X.mean(axis=0)?

Answer it first, then check.

Hint

Remove the axis-0 length from (5, 3).

Solution

The shape is (3,). Reducing over the five examples removes axis 0 and leaves one mean for each of the three features.