NumPy Flatten

The numpy.ndarray.flatten() in the NumPy array library is used to flatten an array of elements in different orders.

The flattening of a NumPy n-dimensional array in python is a procedure to return a copy of an array that is collapsed to a one-dimensional numpy array. 

The flattening of the NumPy array can be done in 4 different orders, those are:

  1. C-style or Row-Major style Order: the ordering of consecutive row elements residing next to each other.
  2. F-Style (Fortran Style) or Column-Major Order: the ordering of consecutive column elements residing next to each other.
  3. A-style: The ordering can be two types here. 1) column-major order if the numpy array is Fortran Contiguous, 2)Row-Major order if the numpy array is Fortran Non-Contiguous.
  4. K-Style Order: flatten in the order that elements occur in memory.

The numpy.ndarray.flatten() method will return the copy of the original array. Since the n-dimensional output array is a copy of the original numpy array, changing the output array will not affect the value of the original numpy array.

ndarray.flatten() method Syntax

The syntax of numpy.ndarray.flatten() method is:

ndarray.flatten(order='C')

ndarray.flatten() method Parameters

The numpy.ndarray.flatten() method takes a single optional argument.

  • order: {‘C’,’F’, ‘A’,’K’}: (Optional)– the type of order of flattening which can be any of the values as follows {‘C’,’F’,’A’,’K’}. If no value is provided it takes the default value ‘C’ which is row-major style order.

ndarray.flatten() method Return Value

The method ndarray.flatten() returns a copy of a 1-dimensional array with the same type of elements.

Example 1: How to flatten an array using NumPy ndarray.flatten() method

The method ndarray.flatten() flattens the input array into a single-dimensional array. If the order argument is not provided or if the order value is passed as None, then the array returns would be default row-major ordering style also called C-Style ordering.

import numpy as np

# initialize the numpy n-D array
arr_in = np.array([[[2, 17], [45, 78]], [[88, 92], [60, 76]], [[76, 33], [20, 18]]])
print("The input array is:\n", arr_in)

print("The flattened numpy array in default order(C-style order) is:")

# flatten function will flatten input array to default ordering(C-)
print(arr_in.flatten())

Output

The input array is:
 [[[ 2 17]
  [45 78]]

 [[88 92]
  [60 76]]

 [[76 33]
  [20 18]]]

The flattened numpy array in default order(C-style order) is:
[ 2 17 45 78 88 92 60 76 76 33 20 18]

Example 2: Flattening an array using numpy.flatten() method with C-style ordering

If we pass the order='C' as the argument to the method ndarray.flatten(), the method flattens the input array into a single-dimensional array. and returns the array in row-major order style.

import numpy as np

# initialize the numpy n-D array
arr_in = np.array([[[2, 17], [45, 78]], [[88, 92], [60, 76]], [[76, 33], [20, 18]]])
print("The input array is:\n", arr_in)

print(
    "The flattened numpy array in Row major order(C-style order) using order = 'C' is:"
)

# flatten row major ordering using order = 'C'
print(arr_in.flatten(order="C"))

Output

The input array is:
 [[[ 2 17]
  [45 78]]

 [[88 92]
  [60 76]]

 [[76 33]
  [20 18]]]

The flattened numpy array in Row major order(C-style order) using order = 'C' is:
[ 2 17 45 78 88 92 60 76 76 33 20 18]

Example 3: Flattening an array using numpy.flatten() method with Fortran-style ordering

If we pass the order='F' as the argument to the method ndarray.flatten(), the method flattens the input array into a single-dimensional array. and returns the array in column-major order style also called as F-Style (Fortan Style).

import numpy as np

# initialize the numpy n-D array
arr_in = np.array([[[2, 17], [45, 78]], [[88, 92], [60, 76]], [[76, 33], [20, 18]]])
print("The input array is:\n", arr_in)

print(
    "The flattened numpy array using fortran order(Column major order) using order = 'F' is:"
)

# flatten fortran ordering using order = 'F'
print(arr_in.flatten(order="F"))

Output

The input array is:
 [[[ 2 17]
  [45 78]]

 [[88 92]
  [60 76]]

 [[76 33]
  [20 18]]]

The flattened numpy array using fortran order(Column major order) using order = 'F' is:
[ 2 88 76 45 60 20 17 92 33 78 76 18]

Example 4: Flattening an array using numpy.flatten() method with A-style ordering

If we pass the order='A' as the argument to the method ndarray.flatten(), the method flattens the input array into a single-dimensional array. and returns the array in Fortran contiguous ordering in memory.

import numpy as np

# initialize the numpy n-D array
arr_in = np.array([[[2, 17], [45, 78]], [[88, 92], [60, 76]], [[76, 33], [20, 18]]])
print("The input array is:\n", arr_in)

print(
    "The flattened numpy array in column-major order if array is Fortran contiguous in memory using order = 'A'is:"
)

# flattening numpy array in column-major order if array is Fortran contiguous in memory
print(arr_in.flatten(order="A"))

Output

The input array is:
 [[[ 2 17]
  [45 78]]

 [[88 92]
  [60 76]]

 [[76 33]
  [20 18]]]

The flattened numpy array in column-major order if array is Fortran contiguous in memory using order = 'A'is:
[ 2 17 45 78 88 92 60 76 76 33 20 18]

Example 5: Flattening an array using numpy.flatten() method with K-style ordering

If we pass the order='K' as the argument to the method ndarray.flatten(), the method flattens the input array into a single-dimensional array. and returns the 1-D array as elements occur in memory either in contiguous or non-contiguous.

import numpy as np

# initialize the numpy n-D array
arr_in = np.array([[[2, 17], [45, 78]], [[88, 92], [60, 76]], [[76, 33], [20, 18]]])
print("The input array is:\n", arr_in)

print("The array is flattened to order of elements occurs in memory is:")

# flattened to order of elements occurs in memory
print(arr_in.flatten(order="K"))

Output

The input array is:
 [[[ 2 17]
  [45 78]]

 [[88 92]
  [60 76]]

 [[76 33]
  [20 18]]]

The array is flattened to order of elements occurs in memory is:
[ 2 17 45 78 88 92 60 76 76 33 20 18]

Conclusion

The flattening of a NumPy n-dimensional array in python is a procedure to return an array that is collapsed to a one-dimensional numpy array. The function used for flattening a numpy nd array is numpy.-ndarray.flatten().

Leave a Reply

Your email address will not be published.

Sign Up for Our Newsletters

Subscribe to get notified of the latest articles. We will never spam you. Be a part of our ever-growing community.

You May Also Like
Python Dictionary Get()

Python Dictionary get()

Table of Contents Hide Syntax of Dictionary get() get() ParametersReturn Value from Dictionary get()Example 1 – How to use get() method in DictionaryExample 2 – Difference between get() method and dict[key]…
View Post
Python Print Variable

Python Print Variable

Table of Contents Hide How to use the print() function in Python?How to Print variable in Python?Method 1: Using comma , character to separate the variables in a print statementMethod…
View Post