Updated April 10, 2023
Introduction of NumPy flatten
In Python, NumPy flatten function is defined as to flatten the given array of any 2- dimensional or any other multi-dimensional array into a one-dimensional array which is provided by the Python module NumPy and this function is used to return the reduced copy of the array into a one-dimensional array from any multi-dimensional array which is known as flattening function to flatten from multidimensional array to one-dimensional array which also helps to flatten the array in a row and column-wise using the order parameters like C for row-wise flattening of the array and F for column-wise flattening of the array to one dimensional and there are many other options also.
Working of NumPy flatten() Function
In this article, we will see how flatten() function will work in Python with numpy module and an array object defined by this module which is used for representing a multidimensional array having an immovable number of elements in it. This NumPy module and the object of an array as ndarray are used for demonstrating a flatten() function. Now in the below section let us see the syntax and examples of the flatten function.
In the above syntax, we have an array object as ndarray which is an input array to which the flatten() function needs to be applied. The parameter “order” is passed to the function having different values such as C, F, A, and K where C is a default value. This function returns the input array with multi-dimensional array flattens or collapsed to one dimension.
- “C” is used as the value to flatten the given multidimensional array to a single dimension in row-wise order.
- “F” is used to flatten or collapse to single dimensional array in the order of column-wise.
- “A” is optional value and it is used only when the F value is having an array as contiguous in memory with column-wise flattening, otherwise, it will flatten in the row-wise.
- “K” is also used as a value to the order parameter to flatten the array in the order the items or elements appear in the memory.
Example of NumPy flatten
Now let us demonstrate the flatten function with simple example in the below section.
import numpy as nf print("Program to demonstrate flatten function of numpy module") print("\n") in_arr = nf.array([[2,3,8], [4,5,9], [3,0,6]]) print("The given input array is as follows:") print(in_arr) print("\n") print("The flattened array is as follows:") print(in_arr.flatten()) print("\n") print("The flattened array in C ordering is as follows:") print(in_arr.flatten(order = 'C')) print("\n") print("The flattened array in F ordering is as follows:") print(in_arr.flatten(order = 'F')) print("\n") print("The flattened array in A ordering is as follows:") print(in_arr.flatten(order = 'A')) print("\n") print("The flattened array in K ordering is as follows:") print(in_arr.flatten(order = 'K')) print("\n")
In the above program, we can see we have to first import NumPy and set its alias name as “nf” so that it becomes easy to refer it in the entire program. Then we are creating an array of 3*3 dimensional array using an array function of the NumPy module. Then we can flatten this multidimensional 3*3 array into a single dimension array using the flatten() function on the given input array “in_arr”. As C is default value so when we don’t pass any value to the flatten function then it will arrange the 3*3 dimensional array row-wise that means the first row is appended with the second row and then the second is appended wit 3rd row to form a single array. Similarly, when we have specified the value of order as “C” to arrange the multidimensional array to one dimension. Then in the next line, we have specified the value as “F” which is Fortran style to arrange the given input array column-wise as shown in the above screenshot such as the first column is appended with the second column and then it is appended with 3rd column to form 3*3 dimensional array to single dimensional array. So, therefore, as we can see in the above program, we have also used “A” value which is also arranging the elements of 3*3 dimensional array into a one-dimensional array in row-wise as F does not contain contiguous values in memory. Then lastly, we have specified value as “K” which will flatten the elements of 3*3 dimensional array into one dimension as they appear in the given input array memory and the output of all the possibilities of the parameter values are displayed in the above screenshot.
This flatten() function of NumPy which is used in dealing with huge data related to neural networks like convent then this function is very useful. In Python, there is another function provided by the NumPy module is reshape() function which is also similar to flatten() function and also have the same parameter values such as C, F, A and K with C as default for reshaping the elements in row-wise but flatten() function will reduce the multidimensional array to one dimension whereas reshape() function can be used to reduce or collapse or reshape the given array into any dimensional array which is mainly used to arrange data from wide to long.
In this article, we conclude that we are using a flatten() function of the NumPy module in Python to reduce or collapse the given multidimensional array into one dimension array. In this article, we have discussed the syntax along with parameters and its values such as C, F, A, and K which is used for arranging the array elements in either row-wise or column-wise. In this article, we also an example of how to use flatten() function, and their different parameter values are demonstrated with output and screenshot.
Recommend ed Articles
This is a guide to NumPy Flatten. Here we also discuss the introduction and working of numpy flatten() function along with an example. You may also have a look at the following articles to learn more –