# 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

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 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.