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:
- Line the two shapes up on the right.
- 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.
- Every 1 stretches to match the size across from it.
Here is the check for both examples, and for a third that fails:
scores + bonusrowscolumns
scores23bonusnone3checknone: stretchesequalresult23scores - bestrowscolumns
scores23best21checkequal1 stretchesresult23scores - scores.max(axis=1)rowscolumns
scores23scores.max(axis=1)none2checknone: stretches3 vs 2resulterrorLining 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.