Center and Check Measurements
Subtract one mean per sensor and verify the centered values, shape, axes, and near-zero sensor means.
Different sensors may work on different numerical levels. In our small table,
the first sensor reports values near 3, while the second reports values near
8. If a later calculation should describe change around each sensor's own
level, we can subtract one mean from each column. This operation is called
centering.
We begin with the raw table and the calibration from the preceding lessons:
[[ 2. 6.]
[ 3. 8.]
[ 4. 10.]]
Both arrays have shape (3, 2): axis 0 means observations and axis 1
means sensors. Centering should preserve that layout.
Find One Mean per Sensor
Each sensor occupies one column. We therefore average across the observation
axis, axis=0:
[3. 8.]
(2,)
The reduction removes the observation axis. One result remains for each
sensor, so the result has shape (2,).
We can check the first result by hand. The first sensor contains 2, 3, and
4, so its mean is
The second sensor contains 6, 8, and 10, whose mean is 8. The hand
calculation confirms both the values and the axis choice. Using axis=1
instead would calculate one mean per observation, which answers a different
question.
Q1. Choose the reduction axis
The table has axes (observations, sensors). Which expression calculates one
mean for each sensor?
Select one choice, then check.
HintName the axis that should disappear
Observations are on axis 0. Averaging over that axis leaves the sensor
axis.
SolutionReduce across observations
Use calibrated.mean(axis=0). It returns [3.0, 8.0] with shape (2,).
Subtract the Matching Mean from Each Column
The two means align with the two sensor columns. Broadcasting applies them to every observation row:
[[-1. -2.]
[ 0. 0.]
[ 1. 2.]]
(3, 2)
The first centered value is 2 - 3 = -1. The value beside it is 6 - 8 = -2. The middle observation equals the two means, so it becomes [0, 0].
The last observation lies above both means and becomes [1, 2].
The operation changes the values but not the axis meanings. Rows are still
observations, columns are still sensors, and the result keeps shape (3, 2).
The numbers now describe each reading's signed difference from its own sensor
mean.
Q2. Check one centered value
What is the centered value for the last observation of the second sensor?
Compute it first, then check your number.
HintUse the matching column mean
The last reading in the second sensor is 10, and that sensor's mean is
8.
SolutionSubtract the second sensor mean
The centered value is 10 - 8 = 2.
Keep an Axis When It Makes Alignment Easier to See
The shape (2,) broadcasts correctly across a (3, 2) table. Sometimes it is
useful to keep the reduced observation axis as a length-one axis:
[[3. 8.]]
(1, 2)
Now the shape itself reads as one row by two sensors. The length-one observation axis can expand across the three observations during subtraction:
centered = calibrated - sensor_means
This produces the same (3, 2) values as before. keepdims=True is useful
here because it makes the alignment visible. It is not a setting to add to
every reduction. If the shorter shape (2,) is already clear, it is enough.
Check the Property Centering Should Create
After subtracting each sensor's mean, the mean of each centered column should be zero:
[0. 0.]
True
For this table, the result prints as exact zeros. With other floating-point
values, a calculation that should be zero may leave a tiny value close to
zero. np.allclose checks whether corresponding values are sufficiently near
under a numerical tolerance. That makes it more suitable than exact equality
for this floating-point property.
The check does more than repeat the code. It states the result we expect from centering: one near-zero mean per sensor. We should still check the result's shape and axis meaning, because near-zero numbers on the wrong axis would not answer the intended question.
Q3. Center the columns and check their means
Complete the program so it keeps the reduced axis, centers each sensor column, and checks for one near-zero mean per sensor.
Editable Python
Ready to run.
HintPreserve, subtract, then reduce again
Use mean(axis=0, keepdims=True), subtract sensor_means from
calibrated, and pass centered.mean(axis=0) with np.zeros(2) to
np.allclose.
SolutionCheck the centering invariant
The result keeps shape (3, 2), and near_zero is True because each
sensor column is centered around zero.
Per-sensor centering reduces across observations, broadcasts one mean back
to each matching column, and preserves the table's axes. A hand calculation
checks one value; np.allclose checks the near-zero column-mean property.
The next lesson writes the same numerical work as a loop and as a whole-array
expression, then compares their results.