Complete the code to create a 2D NumPy array with shape (3, 4).
import numpy as np arr = np.array([1]) print(arr.shape)
The array must be a list of lists to create a 2D array with shape (3, 4). Option B matches this shape.
Complete the code to get the strides of the NumPy array 'arr'.
import numpy as np arr = np.array([[1, 2], [3, 4], [5, 6]]) strides = arr.[1] print(strides)
shape instead of strides.The strides attribute shows how many bytes to step in each dimension when traversing the array.
Fix the error in the code to correctly reshape the array without copying data.
import numpy as np arr = np.arange(6) reshaped = arr.[1]((2, 3), order='C') print(reshaped)
resize which modifies the array in-place.flatten or ravel which change the array to 1D.The reshape method returns a new view or copy with the new shape without changing the original data layout.
Fill both blanks to create a transposed view of the array and print its strides.
import numpy as np arr = np.array([[1, 2, 3], [4, 5, 6]]) transposed = arr.[1] print(transposed.[2])
transpose() as a method call instead of property T.shape instead of strides.T is a property that returns the transposed view of the array. The strides attribute shows how data is accessed in memory.
Fill all three blanks to create a dictionary of word lengths for words longer than 3 characters.
words = ['data', 'science', 'is', 'fun'] lengths = { [1]: [2] for word in words if len(word) [3] 3 }
The dictionary comprehension uses the word as key, length as value, and filters words with length greater than 3.