numpy.ndarray.flatten() in Python | np.ndarray.flatten() in Python

Numpy flatten() function is used to transform a multi-dimensional Numpy array to one-dimensional Numpy array. It always allocates new memory for the flattened array.

Syntax of Numpy.ndarray.flatten()

ndarray.flatten()

Python program to test numpy.ndarray.flatten() function

import numpy as np

a = np.array([[23, 76, 11, 42], [74, 91, 8, 34]])
print(a.flatten())

Output of the above program

[23 76 11 42 74 91  8 34]

Let's understand how flatten() function works with a simple diagram. Diagram is in itself self-explanatory-

Numpy flatten() in Python

Recommended Posts