import numpy as np
a = np.arange(9).reshape(3, 3)
print(a.shape)
print(a)
b = np.arange(4, 4+9).reshape(3, 3)
print(b.shape)
print(b)
a+b
So this is easy and one-to-one.
What if the shapes do not match?
a = np.arange(9).reshape(3, 3)
print(a.shape)
print(a)
b = np.arange(3)
print(b.shape)
print(b)
What will this do?
a+b
It has broadcast along the last axis!
Can we broadcast along the first axis?
a+b[:, np.newaxis]
What's the shape of the newaxis
thing?
b[:, np.newaxis].shape
Rules: