Complete the code to add a 1D array to each row of a 2D array using broadcasting.
import numpy as np arr_2d = np.array([[1, 2, 3], [4, 5, 6]]) arr_1d = np.array([10, 20, 30]) result = arr_2d + [1] print(result)
Adding arr_1d to arr_2d broadcasts the 1D array across each row of the 2D array.
Complete the code to multiply a 2D array by a 1D array using broadcasting along columns.
import numpy as np arr_2d = np.array([[1, 2, 3], [4, 5, 6]]) arr_1d = np.array([2, 3]) result = arr_2d * [1] print(result)
Reshaping arr_1d to (2,1) allows broadcasting along columns to multiply each row by the corresponding element.
Complete the code to add a 1D array to each column of a 2D array using broadcasting.
import numpy as np arr_2d = np.array([[1, 2], [3, 4], [5, 6]]) arr_1d = np.array([10, 20, 30]) result = arr_2d + [1] print(result)
arr_1d directly, causing a broadcasting error.Reshaping arr_1d to (3,1) broadcasts it across the columns (second dimension) of arr_2d.
Fix the error in the code to subtract a 1D array from each column of a 2D array using broadcasting.
import numpy as np arr_2d = np.array([[5, 10, 15], [20, 25, 30]]) arr_1d = np.array([1, 2]) result = arr_2d - [1] print(result)
Reshaping arr_1d to (2,1) aligns it with the rows of arr_2d for broadcasting subtraction along columns.
Complete the code to compute the outer product of two arrays using broadcasting (no explicit reshape needed).
import numpy as np a = np.array([[1], [2]]) b = np.array([[10, 20, 30]]) result = a * [1] print(result)
The shapes (2,1) and (1,3) are compatible for broadcasting: the 1s are expanded to match, resulting in (2,3) outer product multiplication.