numpy.ndarray.flatten() in Python
The numpy.ndarray.flatten() returns a copy of the array collapsed into 1-dimension.
Syntax
ndarray.flatten(order='C')
Parameter
The numpy. ndarray.flatten() method consists of one parameter, which is as follows:
order : This parameter can be either C_contiguous or F_contiguous where C order operates row-rise on the array, and F order operates column-wise operations.
Return
This function returns a copy of the input array, flattened to one dimension.
Example 1
# Python Program explaining
# numpy.ndarray.flatten() function
import numpy as np
arr = np.arange(12).reshape(3,4)
print ('Original array:' )
print (arr)
# default is column-major
print ('The flattened array:' )
print (arr.flatten() )
print ('The flattened array in F-style ordering:')
print (arr.flatten(order = 'F'))
Output
Original array:
[[ 0 1 2 3]
[ 4 5 6 7]
[ 8 9 10 11]]
The flattened array:
[ 0 1 2 3 4 5 6 7 8 9 10 11]
The flattened array in F-style ordering:
[ 0 4 8 1 5 9 2 6 10 3 7 11]