Complete the code to reshape the 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. Here, it changes a 1D array into a 2D array with 3 rows and 2 columns.
Complete the code to reshape the array into 3 rows and 2 columns.
import numpy as np arr = np.arange(6) new_arr = arr.[1]((3, 2)) print(new_arr)
resize which modifies the original array.flatten which makes the array 1D.reshape() changes the shape of the array to the given dimensions (3 rows, 2 columns).
Fix the error in reshaping the array to 2 rows and 4 columns.
import numpy as np arr = np.array([1, 2, 3, 4, 5, 6]) arr.[1]((2, 4)) print(arr)
reshape with incompatible shape causes an error.flatten or ravel does not change shape as needed.The error is because the total size (6) does not match the new shape (2*4=8). Using resize() changes the array size and fills with zeros if needed.
Fill both blanks to create a 3D array with shape (2, 3, 1).
import numpy as np arr = np.arange(6) reshaped = arr.[1]((2, [2], 1)) print(reshaped.shape)
resize which changes array size.Use reshape to change the array shape to (2, 3, 1). The middle dimension is 3 to match total elements (2*3*1=6).
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)
This dictionary comprehension creates a dictionary where keys are words and values are their lengths, but only for words longer than 3 letters.