Open In Colab

13: Numpy and Broadcasting#

import numpy as np
A = np.ones((2, 3))
B = 3
print(A+B) # result 1
[[4. 4. 4.]
 [4. 4. 4.]]
B = np.ones((2, 1))
B
array([[1.],
       [1.]])
A = np.ones((2, 3))
B = np.ones((2, 1)) # B has one column
print(A+B) # result 2
[[2. 2. 2.]
 [2. 2. 2.]]
A = np.ones((2, 3))
B = np.ones((1, 3))
print(A+B) # result 3
[[2. 2. 2.]
 [2. 2. 2.]]