Complete the code to create an outer product of two 1D arrays using broadcasting.
import numpy as np x = np.array([1, 2, 3]) y = np.array([4, 5, 6]) outer = x[:, [1]] * y print(outer)
reshape(1, 3) instead of adding a new axis.Using np.newaxis adds a new axis to x, making it a column vector. This allows broadcasting to multiply it with y as a row vector, producing the outer product.
Complete the code to compute the outer product of two arrays using broadcasting with shape manipulation.
import numpy as np a = np.array([2, 3]) b = np.array([5, 7, 11]) result = a.[1] * b print(result)
a incorrectly as (1, 2) which makes it a row vector.a at all, causing shape mismatch.Reshaping a to (2, 1) makes it a column vector. Multiplying by b (shape (3,)) broadcasts b as a row vector, producing a (2, 3) outer product.
Fix the error in the code to correctly compute the outer product using broadcasting.
import numpy as np x = np.array([1, 2, 3]) y = np.array([4, 5, 6]) outer = x.[1] * y print(outer)
reshape(3, 3) which changes the data shape incorrectly.flatten() which does not add a new axis.Reshaping x to (3, 1) makes it a column vector. This allows broadcasting with y (shape (3,)) to compute the outer product.
Fill both blanks to create an outer product using broadcasting with reshaping.
import numpy as np p = np.array([7, 8]) q = np.array([1, 2, 3]) outer_product = p.[1] * q.[2] print(outer_product)
flatten() which removes dimensions instead of reshaping.Reshape p to (2, 1) to make it a column vector, and q to (1, 3) to make it a row vector. Multiplying them broadcasts to a (2, 3) outer product.
Fill both blanks to create a dictionary comprehension that maps each word to its length only if the length is greater than 3.
words = ['data', 'science', 'ai', 'ml'] lengths = {word: {BLANK_2}} for word in words if {{BLANK_2}}
The dictionary comprehension uses word as the key (no extra syntax needed, so blank 1 is empty), len(word) as the value, and filters words with length greater than 3.