Centering with Reductions

Center an array by reducing one axis to compute means and broadcasting those means back across the original values. Use axis meaning to predict both intermediate and final shapes.

Centering subtracts a mean so that values describe differences from that mean. For a table of examples and features, the usual goal is to center each feature separately.

The source has shape (3, 2): three examples and two features. feature_means has shape (2,), one mean per feature. Broadcasting subtracts those two means from every row, so centered keeps shape (3, 2).

Compute the Means by Hand

The first feature mean is:

(1 + 3 + 5) / 3 = 3

The second feature mean is:

(10 + 14 + 18) / 3 = 14

Therefore:

feature_means = [3.0, 14.0]

Subtracting these values from each row gives:

[[-2, -4],
 [ 0,  0],
 [ 2,  4]]

Read Centered Values as Differences from the Mean

Before centering, the first feature contains [1.0, 3.0, 5.0]. After subtracting its mean, it contains [-2.0, 0.0, 2.0]. These values have a direct interpretation:

  • -2.0 means two units below the feature mean;
  • 0.0 means equal to the feature mean;
  • 2.0 means two units above the feature mean.

Centering changes the reference point, but it does not change differences between values. The distance from 1.0 to 5.0 is 4.0, and the distance from -2.0 to 2.0 is also 4.0. It also does not make different feature scales equal; that requires an additional scaling step.

Center each feature

Predict the means and centered values, then change one source value and trace the affected feature.

Ready to run.

The centered feature means are zero, apart from possible tiny floating-point rounding differences in other examples.

Choose the Axis from the Intended Groups

Reducing axis=0 combines examples and leaves one mean for each feature:

(examples, features) -> (features,)

Reducing axis=1 combines features and leaves one mean for each example:

(examples, features) -> (examples,)

The correct axis follows from what should be centered. Feature centering uses one mean per feature. Row centering uses one mean per row.

Keep a Row Mean as a Column

To center every row separately, keep one mean beside each row. Without keepdims=True, data.mean(axis=1) has shape (examples,). Broadcasting aligns that flat result with the final, feature axis—not automatically with the example axis. It fails when the two lengths differ and can express the wrong calculation when they happen to be equal.

Preserve the reduced axis with length one:

For data.shape == (3, 2), row_means.shape is (3, 1). The length-one feature axis expands across each row, and row_centered.shape remains (3, 2). For the example data:

row_means =
[[ 5.5],
 [ 8.5],
 [11.5]]

row_centered =
[[-4.5, 4.5],
 [-5.5, 5.5],
 [-6.5, 6.5]]

Feature centering and row centering answer different questions. Choose between them from the meaning of the axes, not from which expression happens to run.

Check the Result

Do not stop after the subtraction runs. Verify the property that centering was supposed to create:

print(centered.mean(axis=0))

For feature-centered data, each feature mean should be zero or very close to zero. This check tests the meaning of the operation, not only its shape.

Exercise: Predict feature-mean shape

If data.shape is (8, 3), what is the shape of data.mean(axis=0)?

Choose the mean shape

Select one choice, then check.

HintRemove the example axis

Axis 0 has length 8.

SolutionThe result shape is (3,)

Combining eight examples removes axis 0 and leaves one value for each of the three features.

Exercise: Predict centered shape

If data.shape is (8, 3) and feature_means.shape is (3,), what is the shape of data - feature_means?

Choose the centered shape

Select one choice, then check.

HintReuse one mean per feature

Align (8, 3) and (3,) from the right.

SolutionThe result shape is (8, 3)

The last dimensions match, and the missing leading dimension expands across the eight examples.

Exercise: Center one feature

What values result from centering [1.0, 3.0, 5.0] by its mean?

Choose the centered values

Select one choice, then check.

HintThe mean is 3.0

Compute 1 - 3, 3 - 3, and 5 - 3.

SolutionThe centered values are -2, 0, and 2

Subtracting the mean 3.0 produces [-2.0, 0.0, 2.0].

Exercise: Center feature columns

Complete the program so it subtracts one mean per feature and prints feature means close to [0. 0.].

Reduce, broadcast, and verify

Ready to run.

HintKeep one value per feature

Use data.mean(axis=0), then data - feature_means.

SolutionSubtract the feature means

Set feature_means = data.mean(axis=0) and centered = data - feature_means.

Exercise: Preserve row means for broadcasting

If data.shape is (5, 4), which expression produces one row mean in a shape that can be subtracted from every value in that row?

Choose the row-mean expression

Select one choice, then check.

HintKeep a length-one feature axis

The required mean shape is (5, 1).

SolutionUse keepdims=True

data.mean(axis=1, keepdims=True) produces shape (5, 1), which broadcasts across a (5, 4) array.