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.
View all posts