Complete the code to create a 2D array of shape (3, 3) filled with ones using NumPy.
import numpy as np arr = np.[1]((3, 3))
The np.ones function creates an array filled with ones. This is useful to test broadcasting performance with non-zero values.
Complete the code to add a 1D array to each row of a 2D array using broadcasting.
import numpy as np arr2d = np.ones((3, 3)) arr1d = np.array([1, 2, 3]) result = arr2d [1] arr1d
Using the + operator adds the 1D array to each row of the 2D array by broadcasting.
Fix the error in the code to correctly broadcast a 1D array to a 2D array for multiplication.
import numpy as np arr2d = np.ones((3, 3)) arr1d = np.array([1, 2, 3]) result = arr2d * arr1d.[1]((3, 1))
Using reshape changes the shape of arr1d to (3,1) so it broadcasts correctly with arr2d.
Fill both blanks to create a dictionary comprehension that maps words to their lengths only if length is greater than 3.
words = ['data', 'science', 'ai', 'ml'] lengths = {word: [1] for word in words if [2]
The dictionary comprehension uses len(word) as the value and filters words with length greater than 3.
Fill all three blanks to create a dictionary comprehension that maps uppercase words to their lengths only if length is greater than 2.
words = ['cat', 'dog', 'a', 'bird'] result = { [1]: [2] for w in words if [3] }
w.lower() instead of uppercase.The comprehension maps the uppercase version of each word to its length, filtering words longer than 2 characters.