Complete the code to reshape a 1D array into a 2D array with 3 rows.
import numpy as np arr = np.array([1, 2, 3, 4, 5, 6]) reshaped = arr.[1]((3, 2)) print(reshaped)
resize instead of reshape.flatten which converts to 1D instead of reshaping.The reshape method changes the shape of the array without changing its data.
Complete the code to reshape a 2D array into a 1D array.
import numpy as np arr = np.array([[1, 2], [3, 4], [5, 6]]) flat = arr.[1]() print(flat)
reshape without specifying shape.resize which modifies the original array.The flatten method returns a copy of the array collapsed into one dimension.
Fix the error in reshaping an array with incompatible size.
import numpy as np arr = np.array([1, 2, 3, 4, 5]) reshaped = arr.[1]((2, 3)) print(reshaped)
resize which changes data and size.The error is due to shape (2, 3) not matching total elements (5). Use reshape with compatible shape like (1, 5) or (5, 1).
Fill both blanks to create a dictionary with word lengths for words longer than 3 letters.
words = ['data', 'science', 'is', 'fun'] lengths = {word: [1] for word in words if len(word) [2] 3} print(lengths)
The dictionary comprehension uses len(word) for values and filters words with length greater than 3.
Fill all three blanks to create a dictionary with uppercase keys and values greater than 0.
data = {'a': 1, 'b': -2, 'c': 3}
result = { [1]: [2] for k, v in data.items() if v [3] 0 }
print(result)The dictionary comprehension uses uppercase keys with k.upper(), values v, and filters values greater than 0.