Issue: Flattening a 2D array with np.flatten() does not produce the expected output of a 1D array when using Python 3.7.4.
np.flatten()
Code:
import numpy as np array1 = np.array([[1, 2, 3, 2, 5, 8], [9, 5, 1, 7, 5, 3]]) array1.flatten() print(array1)
Current Output:
[[1 2 3 2 5 8] [9 5 1 7 5 3]]
Expected Output:
[1 2 3 2 5 8 9 5 1 7 5 3]
Updated Code:
import numpy as np array1 = np.array([[1, 2, 3, 2, 5, 8], [9, 5, 1, 7, 5, 3]]) flatten_array = array1.flatten() print(flatten_array)
Answer: The np.flatten() method does not modify the original array, but returns a copy of the flattened array. Therefore, the expected output is obtained by assigning the flattened array to a new variable flatten_array and then printing it.
flatten_array