6 Ways to Use Numpy flatten() Method in Python

Introduction

In Python, there are many ways to re-structure the array according to the need of the person. But, there are some cases when we need a one-dimensional array rather than a two-dimensional array. In this type of problem, numpy library provides a function by which we can convert a two-dimensional array into a one-dimensional array, i.e., numpy.ndarray.flatten().

flatten numpy array

What is Numpy Flatten() method?

Numpy.ndarray.flatten() is used when we return the copy of the array in a 1-D rather than a 2-D or multi-dimensional array.

Syntax

ndarray.flatten(order='C')  

Parameter

order: {‘C,’ ‘F,’ ‘A,’ ‘K’} – It is optional input in the function. Firstly, in this parameter, if order = ‘C,’ the array will get flattened in row-major order. Secondly, If order = ‘F’ – It means that the array will get flattened in column-major order.

Thirdly, If order = ‘A’ – It means to flatten in column-major order if Fortran is contiguous in memory. Otherwise, it will be row-major order. At last, If order = ‘K’ – It means that the array will get flattened in the same as it is there in the memory. By default, always the parameter is set to ‘C.’

Return value

y: ndarray – This function returns a copy of the input array, which gets flattened into a one-dimensional.

Examples

Let us understand the numpy flatten() function of the numpy module in detail with the help of examples:

1. Using a 2-D array

In this example, we will be importing the numpy module as np. Then, we will take the input array as a 2-D array. Finally, we will apply the numpy flatten() function to convert a 2-D array into a 1-D array.

#python program using a 2-d array
#Import numpy library 

import numpy as np 
 
arr = np.array([[1,4,7], [2,5,8]]) 
 
output=arr.flatten()  

print("Input Array : ",arr)
print("Output Array : ",output)

Output:

Input Array :  [[1 4 7]
 [2 5 8]]
Output Array :  [1 4 7 2 5 8]

Explanation:

First, we have imported the numpy module with the alias name np. Secondly, we have created a 2-D array using the array function. Thirdly, we have created the variable as output and assigned it the flatten() function. At last, we have printed the value of the output array.

2. Using multi-dimensional array

In this example, we will be importing the numpy module as np. Then, we will take the input array as a multi-dimensional array. Finally, we will apply the numpy flatten() function to convert a multi-dimensional array into a 1-d array.

#python program using a multi-dimensional array
#Import numpy library 

import numpy as np 
 
arr = np.array([[1,4,7], [2,5,8], [12, 58, 36]]) 
 
output=arr.flatten()  

print("Input Array : ",arr)
print("Output Array : ",output)

Output:

Input Array :  [[ 1  4  7]
 [ 2  5  8]
 [12 58 36]]
Output Array :  [ 1  4  7  2  5  8 12 58 36]

Explanation:

First, we have imported the numpy module with the alias name np. Secondly, we have created a multi-dimensional array using an array function. Thirdly, we have created the variable as output and assigned it the numpy flatten() function. At last, we have printed the value of the output array.

3. Using order = ‘F’ as a parameter

In this example, we will be importing the numpy module as np. Then, we will take the input array as a 2-D array. Finally, we will apply the numpy flatten() function with the order = ‘F’ to convert a 2-D array into a 1-D array.

#python program using a 2-d array
#Import numpy library
#using order = 'F' 

import numpy as np 
 
arr = np.array([[1,4,7], [2,5,8]]) 
 
output=arr.flatten('F')  

print("Input Array : ",arr)
print("Output Array : ",output)

Output:

Input Array :  [[1 4 7]
 [2 5 8]]
Output Array :  [1 2 4 5 7 8]

Explanation:

First, we have imported the numpy module with the alias name np. Secondly, we have created a 2-D array using the array function. Thirdly, we have created the variable as output and assigned it the flatten() function. Fourthly, we have used the order ‘F’ to print the output as column-major. At last, we have printed the value of the output array.

4. Using order = ‘C’ as a parameter

In this example, we will be importing the numpy module as np. Then, we will take the input array as a 2-D array. Finally, we will apply the numpy flatten() function with the order = ‘C’ to convert a 2-D array into a 1-D array.

#python program using a 2-d array
#Import numpy library
#using order ='C' 

import numpy as np 
 
arr = np.array([[1,4,7], [2,5,8]]) 
 
output=arr.flatten('C')  

print("Input Array : ",arr)
print("Output Array : ",output)

Output:

Input Array :  [[1 4 7]
 [2 5 8]]
Output Array :  [1 4 7 2 5 8]

Explanation:

First, we have imported the numpy module with the alias name np. Secondly, we have created a 2-D array using the array function. Thirdly, we have created the variable as output and assigned it the value of flatten() function. Fourthly, we have used the order ‘C’ to print the output as row-major. At last, we have printed the value of the output array.

5. using order = ‘A’ as a parameter

In this example, we will be importing the numpy module as np. Then, we will take the input array as a 2-D array. Finally, we will apply the numpy flatten() function with the order = ‘A’ to convert a 2-D array into a 1-D array.

#python program using a 2-d array
#Import numpy library
#using order ='A' 

import numpy as np 
 
arr = np.array([[1,4,7], [2,5,8]]) 
 
output=arr.flatten('A')  

print("Input Array : ",arr)
print("Output Array : ",output)

Output:

Input Array :  [[1 4 7]
 [2 5 8]]
Output Array :  [1 4 7 2 5 8]

Explanation:

First, we have imported the numpy module with the alias name np. Secondly, we have created a 2-D array using the array function. Thirdly, we have created the variable as output and assigned it the flatten() function. Fourthly, we have used the order ‘A’ to print the output as row-major. At last, we have printed the value of the output array.

6. using order = ‘k’ as a parameter

In this example, we will be importing the numpy module as np. Then, we will take the input array as a 2-D array. Finally, we will apply the numpy flatten() function with the order = ‘K’ to convert a 2-D array into a 1-D array.

#python program using a 2-d array
#Import numpy library
#using order ='K' 

import numpy as np 
 
arr = np.array([[21,43,57], [12,85,78]]) 
 
output=arr.flatten('K')  

print("Input Array : ",arr)
print("Output Array : ",output)

Output:

Input Array :  [[21 43 57]
 [12 85 78]]
Output Array :  [21 43 57 12 85 78]

Explanation:

First, we have imported the numpy module with the alias name np. Secondly, we have created a 2-D array using the array function. Thirdly, we have created the variable as output and assigned it the flatten() function. Fourthly, we have used the order ‘K’ to print the output as row-major. At last, we have printed the value of the output array.

Difference between flatten() and ravel()

Both functions are used to convert a multi-dimensional array into a one-dimensional array. But, there are some differences through which they differ in themselves.

Flatten()

  • It always returns a copy of the original array.
  • It is a function of ndarray object.
  • It is slower than ravel() as it takes more memory than ravel().
  • If the value is modified, the original array will not get affected.

Ravel()

  • It always returns the reference of the original array.
  • It is a library-level function.
  • It is faster than flatten().
  • If the value is modified, the original array will also get affected.
#flatten and ravel function in python example

import numpy as np

arr = np.array([[1,2,3], [4,5,6]])
print("Input Array : ",arr)
print("Dimension of array : ",(arr.ndim))
print("\n")

arr1 = arr.ravel()
print("Ravel output : ",arr1)
arr1[0] = 50
print("updated ravel array : ",arr1)
print("Input array : ",arr)
print("\n")

arr2 = arr.flatten()
print("Flatten output : ",arr2)
arr2[0] = 100
print("Updated flatten array : ",arr2)
print("Input array : ",arr)

Output:

Input Array :  [[1 2 3]
 [4 5 6]]
Dimension of array :  2


Ravel output :  [1 2 3 4 5 6]
updated ravel array :  [50  2  3  4  5  6]
Input array :  [[50  2  3]
 [ 4  5  6]]


Flatten output :  [50  2  3  4  5  6]
Updated flatten array :  [100   2   3   4   5   6]
Input array :  [[50  2  3]
 [ 4  5  6]]

Explanation:

Here, firstly, we have imported the numpy module as np. Secondly, we have taken the input array and printed the input array and the array’s dimension. Thirdly, we have applied the ravel() function and printed the ravel() function’s output. We then updated the value of arr2[0] = 50 and printed the ravel output and the input array again. So we can easily see the changes. The same is done with the flatten() function. They also updated the value, but the input array doesn’t change.

Conclusion

In this tutorial, we have discussed the flatten() function of the numpy module. All the parameters and their values are explained in detail with the help of examples. Examples will help you to understand the concept more accurately. You can use the order per your output one-dimensional array requirement.

Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments