If you are looking to transform multi-dimensional Numpy array into a 1-Dimensional array then use Numpy ravel()
function. This function works similar to flatten()
function but there is one difference. The difference is that ravel()
function returns a view of array. If you try to change the value of any element present in the view, then that same change will be reflected in the original array.
np.ravel(array)
array
parameter is used to pass Numpy array that we want to flatten. It is a required parameter.
import numpy as np x = np.array([[23, 76, 11, 42], [74, 91, 8, 34]]) y = np.ravel(x) print('Flattened array: \n', y) y[1] = 121 print('Original array: \n', x)
Output of the above program
Flattened array: [23 76 11 42 74 91 8 34] Original array: [[ 23 121 11 42] [ 74 91 8 34]]
As you can see that we have changed the value of second element to 121 and that change is reflected in the original array x
.