Introduction
In Python Numpy library numpy.ndarray.flatten() is used to flatten multidimensional arrays into onedimensional arrays. In this article, we will explore the syntax, functionality, and various examples of np.flatten() function.
Syntax
The syntax for Numpy flatten() is straightforward as shown below :
numpy.ndarray.flatten(order=â€™Câ€™)
Parameters:
 order (optional): The order parameter determines how elements are arranged in the flattened array. It can take any of the following values:
 â€˜Câ€™: This is the default order and represents rowmajor flattening (also known as Cstyle flattening). The function traverses the array row by row.
 â€˜Fâ€™: This represents columnmajor flattening (Fortranstyle flattening). The function traverses the array column by column.
 â€˜Aâ€™: It flattens in columnmajor order if the array is laid out in Fortran style, otherwise it flattens in rowmajor.
 â€˜Kâ€™: It flattens the array as per how its placed in memory.
Examples of Numpy Flatten Function
Import Library
Let us start with importing the Numpy library as shown below.
In [0]:
import numpy as np
Â
Example 1: Basic Usage of Numpy flatten()
In this example, we have explained the basic usage of the np.flatten() function by flattening a 2D Numpy array into a 1D array.
In [1]:
arr = np.array([[1, 2, 3], Â Â Â Â Â Â Â Â [4, 5, 6]]) flattened_arr = arr.flatten() print(flattened_arr)
Out[1]:
[1 2 3 4 5 6]
Â
Example 2: Flattening 3D Numpy Array
In this example, we are showing how to use numpy.flatten() function to convert 3D Numpy array into 1D array.
In [2]:
arr = np.array([[[1, 2, 3], Â Â Â Â Â Â Â Â Â [4, 5, 6]], Â Â Â Â Â Â Â [[7, 8, 9], Â Â Â Â Â Â Â Â Â [10, 11, 12]]]) flattened_arr = arr.flatten() print(flattened_arr)
Out[2]:
[ 1 2 3 4 5 6 7 8 9 10 11 12]
Â
Example 3: Using flatten() with order=â€™Câ€™
In this example, we use np.flatten() on the 2D Numpy array by using the default order=â€™Câ€™, resulting in a Cordered flattened array. Here the function traverses the array rowwise like 1,2,3,4â€¦ and so on.
In [3]:
arr = np.array([[1, 2, 3], Â Â Â Â Â Â Â Â [4, 5, 6]]) flattened_c_order = arr.flatten(order='C') print("Cordered Flattened Array:", flattened_c_order)
Out[3]:
Cordered Flattened Array: [1 2 3 4 5 6]
Â
Example 4: Using flatten() with order=â€™Fâ€™
Compared to the previous example, this time when we use order=â€™Fâ€™ the function traverses the array column by column like 1,4,2,5.. and so on. This is the FortanStyle flattening.
In [4]:
arr = np.array([[1, 2, 3], Â Â Â Â Â Â Â Â [4, 5, 6]]) flattened_f_order = arr.flatten(order='F') print("Fordered Flattened Array:", flattened_f_order)
Out[4]:
Fordered Flattened Array: [1 4 2 5 3 6]
Â
Example 5: Using flatten() with order=â€™Aâ€™
When we use order=â€™Aâ€™, it behaves in two ways depending on whether the input Numpy array is created as row major or column major. We have two subexamples to show both behaviors â€“
i) Row Major Array
In a rowmajor array, the elements are indexed in rows like 1,2,3,4â€¦ and so on as per the below example. In this case, when order=â€™Aâ€™ is used it traverses the elements rowwise to flatten the array.
In [5]:
arr = np.array([[1, 2, 3], Â Â Â Â Â Â Â Â [4, 5, 6]]) flattened_arr_c = arr.flatten(order='C') print("Aordered Flattened Array:", flattened_arr_a)
Out[5]:
Aordered Flattened Array: [1 2 3 4 5 6]
ii) Column Major Array
In a columnmajor array, the elements the indexed in a columnar manner like 1,4,2,5â€¦ and so on as per the below example. When order=â€™Aâ€™ is used it traverses columnwise to flatten the array.
arr = np.array([[1, 2, 3], Â Â Â Â Â Â Â Â [4, 5, 6]], order='F') Â # Creating a columnmajor (Fortranstyle) array flattened_arr_a = arr.flatten(order='A') print("Aordered Flattened Array:", flattened_arr_a)
Aordered Flattened Array: [1 4 2 5 3 6]
Â

MLK is a knowledge sharing community platform for machine learning enthusiasts, beginners and experts. Let us create a powerful hub together to Make AI Simple for everyone.