NumPy – ndarray. flatten

NumPy - ndarray. flatten

In this chapter, we will discuss about NumPy – ndarray. flatten. This function returns a copy of an array collapsed into one dimension. The function takes the following parameters.

ndarray.flatten(order)

Where,

Sr.No.Parameter & Description
1order− row-major (default. ‘F’: column-major ‘A’: flatten in column-major order, if a is Fortran contiguous in memory, row-major order otherwise ‘K’: flatten an in the order the elements occur in the memory

Example Of NumPy ndarray. flatten

import numpy as np 
a = np.arange(8).reshape(2,4) 

print 'The original array is:' 
print a 
print '\n'  
# default is column-major 

print 'The flattened array is:' 
print a.flatten() 
print '\n'  

print 'The flattened array in F-style ordering:' 
print a.flatten(order = 'F')

The output of the above program would be as follows −

The original array is:
[[0 1 2 3]
 [4 5 6 7]]

The flattened array is:
[0 1 2 3 4 5 6 7]

The flattened array in F-style ordering:
[0 4 1 5 2 6 3 7]

Next Topic – Click Here

This Post Has 2 Comments

Leave a Reply