9/26
Part 2 of 4 · BroadcastingLearn and do

The rule NumPy uses

How does NumPy know which way to copy? It looks only at the two shapes:

  1. Line the two shapes up on the right.
  2. Compare the sizes one position at a time. Two sizes fit if they are equal, or if one of them is 1. A missing size counts as 1.
  3. Every 1 stretches to match the size across from it.

Here is the check for both examples, and for a third that fails:

The rule, on three examples
scores + bonus
rowscolumnsscores23bonusnone3checknone: stretchesequalresult23
scores - best
rowscolumnsscores23best21checkequal1 stretchesresult23
scores - scores.max(axis=1)
rowscolumnsscores23scores.max(axis=1)none2checknone: stretches3 vs 2resulterror

Lining up on the right is why bonus lands on the tests. Its 3 numbers sit under the table's last size, the 3 tests. It has no size for the students, and a missing size stretches.

In the editor, a to e are five pairs of shapes. Work out each result's shape with the rule, and write it as a tuple, or None where the shapes don't fit. Then run it to check.