numpy.ndarray.flatten() in Python


The Python numpy.ndarray.flatten() function returns the copy of array that’s collapsed into a one-dimensional array.

Basic Syntax

Following is the basic syntax for numpy.argmin() function in Python:

numpy.flatten(order=’C’)

And the parameters are:

Parameter Description
order [OPTIONAL], values ca be {‘C’, ‘F’, ‘A’, ‘K’}. Where C means flatten row-major, F means flatten column-major, A means flatten in column-major order if a is Fortran contiguous in memory else row-major order, K means to flatten in order elements occur in memory.

Return Value

This function returns a copy of array collapsed into a one-dimensional array.

Example’s

Following are the examples for numpy.flatten() function:

Example 1

# Python Program illustration of numpy.argmin() function 
import numpy as np 
# 2D array for input 
array = np.array([[1,2],[3,4]]) 
print("The input array: \n", array) 
# flattened array
print("\nThe min element: ", array.flatten()) 

The output for the above program is as given below:

The input array:
[[1, 2], [3, 4]]
The flattened array: [1, 2, 3, 4]

Example 2

# Python Program illustration of numpy.argmin() function 
import numpy as np 
# Generating 2D array for input 
array = np.arange(15).reshape(3, 5) 
print("The input array: \n", array) 
# flattened array
print("\nThe min element: ", array.flatten())  

The output for the above program is as given below:

The input array:
[[ 0, 1, 2, 3, 4],
[ 5, 6, 7, 8, 9],
[10, 11, 12, 13, 14]]
The flattened array: [ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14]