Complete the code to create a 3D array of zeros with shape (2, 3, 4).
import numpy as np arr = np.zeros([1]) print(arr.shape)
The np.zeros function requires a tuple to specify the shape. So (2, 3, 4) is correct.
Complete the code to add a 1D array to a 3D array using broadcasting.
import numpy as np arr3d = np.ones((2, 3, 4)) arr1d = np.array([1, 2, 3, 4]) result = arr3d + [1] print(result.shape)
To broadcast a 1D array over the last dimension of a 3D array, reshape it to (1, 1, 4).
Fix the error in the code to multiply a (3, 1) array with a (1, 4) array using broadcasting.
import numpy as np arr1 = np.array([[1], [2], [3]]) arr2 = np.array([[4, 5, 6, 7]]) result = arr1 [1] arr2 print(result.shape)
Multiplying arrays with shapes (3,1) and (1,4) broadcasts to (3,4). The operator * is correct.
Fill both blanks to create a 3D array by broadcasting a (3, 1) array and a (1, 4) array.
import numpy as np arr1 = np.array([[1], [2], [3]]) arr2 = np.array([[4, 5, 6, 7]]) result = arr1 [1] arr2 print(result.shape == ([2]))
Multiplying arr1 and arr2 broadcasts to shape (3, 4).
Fill all three blanks to multiply two arrays using broadcasting and verify the resulting shape.
import numpy as np a = np.ones((3,1)) b = np.arange(4).reshape(1,4) result = a [1] b print(result.shape == ([2], [3]))
@ instead of element-wise *.The arrays with shapes (3,1) and (1,4) broadcast to (3,4) when using the element-wise multiplication operator *.