Broadcasting
Not met yet
When arrays of different shapes meet in arithmetic, NumPy stretches the smaller one to fit, without copying it. Shapes are compared from the right-hand end: each pair of sizes must match, or one of them must be 1. So (395, 3) minus (3,) takes three assessment averages off every student, but (395, 3) minus (395,) fails. To work row by row, keep the averages as a column with keepdims=True, which gives shape (395, 1).
Try it
main.pyOutput
Press Run and what your program prints shows up here.
Easy to get wrong
- Thinks NumPy lines a one-dimensional array up with whichever axis matches its length. Shapes are compared from the right, so it always meets the last axis.
- Thinks two arrays must have exactly the same shape to be added or subtracted.