Python Numpy Flatten Function [Explained with 4 Examples]

python numpy flatten function parameters with examples python code

In this article, we’ll walk ourselves through Python Numpy flatten function. We’ll first discuss the flatten() function theoretically and after that, we’ll go through multiple code examples in which we’ll explain the working of this function practically for proper understanding.

What is Python Numpy Flatten Function?

python numpy flatten function parameters with examples python code

flatten function of python numpy with examples python code

python numpy flatten function parameters explained with code examples

This function is used to convert a multi-dimensional array to a single dimension array. We’ve discussed the ravel function of Numpy which is kind of similar to the flatten function. The difference between flatten() and ravel() function is that the flatten function always creates a copy of the resulting array in which there is one row and all the items are flattened in it. Whereas the ravel function always tries to avoid making a copy of the output array.

Syntax and Parameter of flatten() Function

numpy.ndarray.flatten(order='C')
  1. This function has a single parameter named ‘order‘ which is optional. We can either pass ‘C‘ (row-major order) or ‘F‘ (column-major order) to it. The default value assigned to it is ‘C‘.
  2. The flatten function returns a 1D/one-dimensional array.

Practical Implementation of Numpy Flatten Function (4 Examples)

Example 1: Flatten a 2D Array

import numpy as np
ar2D = np.array([[11, 22, 33], [44, 55, 66]])
flat_arr = ar2D.flatten()
print(flat_arr)

Output

[11 22 33 44 55 66]

Explanation of Code

  1. We first have imported the Numpy library which is crucial to use the flatten function.
  2. After that, we created a simple 2D Numpy array so it can be converted to a 1D array.
  3. In the third step, we called the flatten() function and store the result inside a variable and prints it.
  4. In the output block, we can see a 2D Numpy converted to 1D using the flatten() function of the Python Numpy library.

Example 2: Flatten function applied on a 3D Array

ar3D = np.array([[[11, 22], [33, 44], [55, 66]], [[77, 88], [99, 100], [111, 122]], [[133, 144], [155, 166], [177, 188]]])
flattened_ar = ar3D.flatten()
print(flattened_ar)

Output

[ 11  22  33  44  55  66  77  88  99 100 111 122 133 144 155 166 177 188]

It’s the same as example 1, but the difference is that we’re using a 3D array to be flattened using the flatten() function. Next, we’ve called the flatten function to create a 1D array from this three-dimensional array. We can see that this function has successfully created a 1D array from the given 3D Numpy array.

Example 3: Changing Order in flatten() Function(Order ‘C’ or ‘F’)

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

flat_arrC = array2D.flatten(order='C')
flat_arrF = array2D.flatten(order='F')

print('This is order C:')
print(flat_arrC)

print('This is order F:')
print(flat_arrF)

Output

This is order C:
[1 2 3 4 5 6]
This is order F:
[1 4 2 5 3 6]

By using the order parameter of flatten function, we can easily change the order of items of the resulting array. It takes only 2 orders.

  • ‘C’(row-major order): It is given to the order parameter by default. It sets the items of the resulting one-dimensional array in a row-wise order.
  • ‘F’(column-major order): It sets the items of the output 1D array in a column-wise order.

We can see in the above output section that although the items are the same in the arrays but their orders are different. It’s because of this order parameter which takes ‘C’ by default, but we’ve tested it with ‘F’ as well.

Example 4: Changing items of Flattened Array

Always remember that the copy of the original array in flatten form is returned by the Numpy flatten function. What it means is that if you do any changes to the flattened array, it won’t change the original array. See the below example:

originalArray2D = np.array([[1, 2, 3,4,5], [6, 7, 8,9,10]])

flattened_arr = originalArray2D.flatten()

flat_arr[4]=760    # modifying flattened array item 5th item as index start from 0

print('Flattened array after modification')
print(flat_arr)

print('Original 2D array after modification in flattened array')
print(originalArray2D)

Output

Flattened array after modification
[ 11  22  33  44 760  66]
Original 2D array after modification in flattened array
[[ 1  2  3  4  5]
 [ 6  7  8  9 10]]

We’ve made use of the indexing method to change the data of the 5th item inside the flattened array. We can see that no modifications have been done to the original array that is used to create the flattened array. Only the flattened array’s item data has been updated. So, it means the Numpy flatten() function creates a flattened copy using the original array.

Conclusion

In conclusion, we now know how to make use of the Python Numpy flatten function properly inside Python code. We’ve gone through multiple scenarios in which we can use this function to make you better understand the working of this function.

Thank you very much for reading it.

Leave a Comment

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