flatten() method in Numpy Python by R4R Team

- flatten() is a Python Array method which take an array of any shape and convert it into single line array.
- it return array of 1xm shape.

Syntax-

array.flatten()

Example-

array is
[[1,2]
[3,4]]

then array.flatten() will return [1,2,3,4]


program-

#import numpy
import numpy as np

l=[[1,2,3],[4,5,6]]
arr=np.array(l)
print("Given array isn",arr)
print("array become",arr.flatten())

l=[[1,2,3],[4,5,6],[1,1,1]]
arr=np.array(l)
print("nGiven array isn",arr)
print("array become",arr.flatten())


output-

Given array is
[[1 2 3]
[4 5 6]]
array become [1 2 3 4 5 6]

Given array is
[[1 2 3]
[4 5 6]
[1 1 1]]
array become [1 2 3 4 5 6 1 1 1]

-In this program, we have a different arrays of order (2,3) and (3,3)
-Then using flatten() function, we convert array into single line array.




Leave a Comment:
Search
Categories
R4R Team
R4Rin Top Tutorials are Core Java,Hibernate ,Spring,Sturts.The content on R4R.in website is done by expert team not only with the help of books but along with the strong professional knowledge in all context like coding,designing, marketing,etc!