Numpy Flatten Tutorial | numpy.ndarray.flatten()

Introduction

In Python Numpy library numpy.ndarray.flatten() is used to flatten multi-dimensional arrays into one-dimensional 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 row-major flattening (also known as C-style flattening). The function traverses the array row by row.
    • ‘F’: This represents column-major flattening (Fortran-style flattening). The function traverses the array column by column.
    • ‘A’: It flattens in column-major order if the array is laid out in Fortran style, otherwise it flattens in row-major.
    • ‘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 C-ordered flattened array. Here the function traverses the array row-wise 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("C-ordered Flattened Array:", flattened_c_order)

Out[3]:

C-ordered 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 Fortan-Style flattening.

In [4]:

arr = np.array([[1, 2, 3],
                [4, 5, 6]])

flattened_f_order = arr.flatten(order='F')
print("F-ordered Flattened Array:", flattened_f_order)

Out[4]:

F-ordered 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 sub-examples to show both behaviors –

i) Row Major Array

In a row-major 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 row-wise to flatten the array.

In [5]:

arr = np.array([[1, 2, 3],
                [4, 5, 6]])

flattened_arr_c = arr.flatten(order='C')
print("A-ordered Flattened Array:", flattened_arr_a)

Out[5]:

A-ordered Flattened Array: [1 2 3 4 5 6]

ii) Column Major Array

In a column-major 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 column-wise to flatten the array.

In [6]:
arr = np.array([[1, 2, 3],
                [4, 5, 6]], order='F')  # Creating a column-major (Fortran-style) array

flattened_arr_a = arr.flatten(order='A')
print("A-ordered Flattened Array:", flattened_arr_a)
Out[6]:
A-ordered Flattened Array: [1 4 2 5 3 6]

 

  • MLK

    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.

Follow Us

Leave a Reply

Your email address will not be published. Required fields are marked *