Complete the code to broadcast array a to shape (3, 3).
import numpy as np a = np.array([1, 2, 3]) b = np.broadcast_to(a, [1]) print(b)
We use np.broadcast_to to expand a to shape (3, 3) by repeating its values along the new axis.
Complete the code to broadcast a 1D array arr to a 2D array with 4 rows and 5 columns.
import numpy as np arr = np.array([10, 20, 30, 40, 50]) broadcasted = np.broadcast_to(arr, [1]) print(broadcasted)
The array arr has 5 elements. Broadcasting to (4, 5) repeats arr across 4 rows.
Fix the error in broadcasting a 1D array data to shape (2, 3).
import numpy as np data = np.array([1, 2, 3]) result = np.broadcast_to(data, [1]) print(result)
The original array has shape (3,), so broadcasting to (2, 3) repeats the array over 2 rows correctly.
Fill both blanks to create a dictionary with words as keys and their lengths broadcasted only if length is greater than 3.
words = ['apple', 'bat', 'carrot', 'dog'] lengths = {word: len(word) for word in words if len(word) [1] 3} broadcasted = np.broadcast_to(np.array(list(lengths.values())), [2]) print(broadcasted)
We filter words with length greater than 3, then broadcast the lengths array to shape (2, 2).
Fill all three blanks to create a dictionary with uppercase words as keys, their lengths as values, and broadcast lengths greater than 4 to shape (3, 2).
words = ['tree', 'house', 'car', 'elephant'] lengths = {word[1]: [2] for word in words if len(word) [3] 4} broadcasted = np.broadcast_to(np.array(list(lengths.values())), (3, 2)) print(broadcasted)
We convert words to uppercase keys, use their lengths as values, and filter lengths greater than 4 before broadcasting.