Align Arrays with Broadcasting

Apply scalar, per-sensor, and per-observation values by aligning compatible shapes with named axes.

One correction may apply to every value, one value per sensor, or one value per observation. NumPy can align these smaller arrays with a measurement table, but the shapes must match both its rules and the meaning of the data.

Begin with Named Axes

The recurring table has three observations and two sensors:

Its shape and axis meanings are:

(observations, sensors) = (3, 2)

Lesson 1 used the single value 0.5 in raw * 0.5. A single value has no observation or sensor axis, so the same multiplication can apply at every position. This is the simplest case of broadcasting: NumPy aligns a smaller value or array with a larger array without requiring us to copy it manually.

Naming the axes comes before checking shapes. A shape can tell us that an operation will run; it cannot tell us whether a correction belongs to sensors, observations, or something else.

Align One Correction per Sensor

Suppose the two columns represent sensors in the order North, West. Their additive offsets are:

The one-dimensional shape (2,) has one entry for the sensor axis. NumPy aligns it with the final axis of raw:

raw             (3, 2)  observations, sensors
sensor_offsets      (2)                sensors

The lengths on the right match: 2 and 2. NumPy reuses the sensor offsets for every observation:

[[ 2.5  9. ]
 [ 4.5 13. ]
 [ 6.5 17. ]]

The North offset 0.5 changes the first column. The West offset -1.0 changes the second column. For the second observation, the visible calculation is:

[4.0, 14.0] + [0.5, -1.0] = [4.5, 13.0]

Q1. Apply one offset per sensor

For the observation [6.0, 18.0], what does adding the sensor offsets [0.5, -1.0] produce?

Choose one

Select one choice, then check.

HintAlign positions on the sensor axis

Calculate 6.0 + 0.5 and 18.0 + (-1.0) separately.

SolutionAdd the two named sensor offsets

The first result is 6.5; the second is 17.0. The adjusted observation is [6.5, 17.0].

Not attempted
Review

Not marked done.

Preserve an Axis with Length One

Now suppose each observation was recorded under a different known offset:

The first axis has one value per observation. The second axis has length 1, which says that the observation's offset should be reused across both sensors:

raw                  (3, 2)  observations, sensors
observation_offsets  (3, 1)  observations, one value to reuse

When NumPy compares shapes from the right, corresponding lengths are compatible if they are equal or if one of them is 1. Here, 1 can expand across the two sensors, and 3 matches the three observations.

[[ 2. 10.]
 [ 5. 15.]
 [ 5. 17.]]

The middle observation receives 1.0 in both columns. The final observation receives -1.0 in both columns.

A flat array with shape (3,) would not express this alignment:

NumPy aligns dimensions from the right, so it first compares the final lengths 2 and 3. Neither is 1, and they are not equal. The operation raises a shape-mismatch ValueError. Reshaping the offsets to (3, 1) both makes the operation compatible and preserves the intended observation axis.

Q2. Shape one value per observation

The flat offsets belong to the three observations. Reshape them so each value is reused across the two sensor columns, then add them to raw.

Editable Python

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

Ready to run.

HintAdd a length-one final axis

Use flat_offsets.reshape(3, 1), then add the reshaped array to raw.

SolutionReshape before adding

Shape (3, 1) names three observations and one value to reuse across the sensor axis. The result retains shape (3, 2).

Not attempted
Review

Not marked done.

Check Meaning After Shape Compatibility

Compatible shapes do not guarantee a correct program. Suppose the table's columns are ordered North, West, but an external file supplies these labelled offsets in the order West, North:

offset_values has shape (2,), so raw + offset_values runs. It is still wrong: the value labelled West is applied to the North column. The values must first be reordered to match the table's sensor order:

This error is different from the incompatible (3,) observation vector. One fails because its right-aligned lengths are 2 and 3. The other runs because both final lengths are 2, but violates the named column order.

For a computation that combines both kinds of correction, inspect each shape and meaning separately:

[[ 2.5  9. ]
 [ 5.5 14. ]
 [ 5.5 16. ]]

The first added array varies across sensors and repeats across observations. The second varies across observations and repeats across sensors. The output keeps the raw table's (observations, sensors) axis meaning.

Q3. Separate shape failure from meaning failure

Which diagnosis is accurate for the (3, 2) table raw?

Choose one

Select one choice, then check.

HintPerform two different checks

First apply the equal-or-one shape rule. If an operation can run, compare the correction labels with the table's named column order.

SolutionCompatibility and meaning are separate

Shape (3,) is incompatible because its final length 3 conflicts with the table's final length 2. Shape (2,) is compatible, but an offset order of West, North is semantically wrong for columns ordered North, West.

Not attempted
Review

Not marked done.

Broadcasting aligns dimensions from the right: lengths must match or one length must be 1. A (2,) vector can carry one value per sensor, while a (3, 1) array can carry one value per observation. Shape compatibility is only the first check; axis names and label order must also agree. The next lesson combines a reduction with broadcasting to center each sensor column.

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.