{"id":5876,"date":"2021-05-13T17:04:23","date_gmt":"2021-05-13T11:34:23","guid":{"rendered":"https:\/\/python-programs.com\/?p=5876"},"modified":"2021-11-22T18:42:41","modified_gmt":"2021-11-22T13:12:41","slug":"python-numpy-flatten-function-tutorial-with-examples","status":"publish","type":"post","link":"https:\/\/python-programs.com\/python-numpy-flatten-function-tutorial-with-examples\/","title":{"rendered":"Python numpy.flatten() Function Tutorial with Examples | How to Use Function Numpy Flatten in Python?"},"content":{"rendered":"
In this tutorial, Beginners and Experience python developers will learn about function numpy.flatten(), how to use it, and how it works. Kindly, hit on the available links and understand how numpy.ndarray.flatten() function in Python gonna help you while programming.<\/p>\n
A numpy array has a member function to flatten its contents or convert an array of any shape to a 1D numpy array,<\/p>\n
Here we can pass the following parameters-<\/p>\n Order: In this, we give an order in which items from the numpy array will be used,<\/p>\n C: Read items from array row-wise<\/p>\n F: Read items from array column-wise<\/p>\n A: Read items from array-based on memory order<\/p>\n It returns a copy of the input array but in a 1D array.<\/p>\n Also Check:<\/span><\/p>\n Let’s learn the concept by viewing the below practical examples,<\/p>\n First of all, import the numpy module,<\/p>\n Let’s suppose, we have a 2D Numpy array,<\/p>\n Output:<\/strong><\/p>\n Now we are going to use the above 2D Numpy array to convert the 1D Numpy array.<\/p>\n Output:<\/strong><\/p>\n So in the above example, you have seen how we converted the 2D array into a 1D array.<\/p>\n flatten() function always returns a copy of the given array means if we make any changes in the returned array will not edit anything in the original one.<\/p>\n output:<\/strong><\/p>\n Thus in the above example, you can see that it has not affected the original array.<\/p>\n It accepts different parameter orders. It can be \u2018C\u2019 or \u2018F\u2019 or \u2018A\u2019, but the default value is \u2018C\u2019. In the below example, we are going to use the same 2D array which we used in the above example-<\/p>\n In this, if we will not pass any parameter in function then it will take ‘C’ as a default value<\/p>\n Output:<\/strong><\/p>\n If we pass \u2018F\u2019 as the order parameter in\u00a0 function then it means elements from a 2D array will be read column wise<\/p>\n Output:<\/strong><\/p>\n Let’s create a transparent view of the given 2D array<\/p>\n Output:<\/strong><\/p>\n Now flatten this view was Row Wise,<\/p>\n Output:<\/strong><\/p>\n Let’s create a 3D numpy array,<\/p>\n Output:<\/strong><\/p>\n Now we are going to flatten this 3D numpy array,<\/p>\n Output:<\/strong><\/p>\n Now, we have to create a list of arrays,<\/p>\n Output:<\/p>\n Now, its time to convert the above list of numpy arrays\u00a0to a flat 1D numpy array,<\/p>\n Output:<\/p>\n To perform this process, first, we have to create a 2D numpy array from a list of list and then convert that to a flat 1D Numpy array,<\/p>\n Output:<\/p>\n Hence, this is how we can use flatten() function in numpy.<\/p>\n We hope this python tutorial, you have seen how to use function numpy.flatten() assist you all in needy times. Thank you! keep visiting our site frequently for updated concepts of python.<\/p>\n","protected":false},"excerpt":{"rendered":" In this tutorial, Beginners and Experience python developers will learn about function numpy.flatten(), how to use it, and how it works. Kindly, hit on the available links and understand how numpy.ndarray.flatten() function in Python gonna help you while programming. numpy.ndarray.flatten() in Python Flatten a matrix or a 2D array to a 1D array using ndarray.flatten() …<\/p>\nndarray.flatten(order='C')<\/code><\/p>\n
Parameters:<\/h3>\n
Returns:<\/h3>\n
\n
<\/a>Flatten a matrix or a 2D array to a 1D array using ndarray.flatten()<\/h2>\n
import numpy as np<\/code><\/p>\n
import numpy as np\r\n\r\n# Create a 2D Numpy array from list of list\r\narr_2d = np.array([[7, 4, 2],\r\n [5, 4, 3],\r\n [9, 7, 1]])\r\nprint(arr_2d)\r\n<\/pre>\n
[7 4 2]\r\n[5 4 3]\r\n[9 7 1]]\r\n<\/pre>\n
import numpy as np\r\n\r\n# Create a 2D Numpy array from list of list\r\narr_2d = np.array([[7, 4, 2],\r\n [5, 4, 3],\r\n [9, 7, 1]])\r\nprint(arr_2d)\r\n# Convert the 2D array to 1D array\r\nflat_array = arr_2d.flatten()\r\nprint('Flattened 1D Numpy Array:')\r\nprint(flat_array)<\/pre>\n
[[7 4 2]\r\n[5 4 3]\r\n[9 7 1]]\r\n\r\nFlattened 1D Numpy Array:\r\n[7 4 2 5 4 3 9 7 1]\r\n\r\n<\/pre>\n
<\/a>ndarray.flatten() returns a copy of the input array<\/h2>\n
import numpy as np\r\n\r\n# Create a 2D Numpy array from list of list\r\narr_2d = np.array([[7, 4, 2],\r\n [5, 4, 3],\r\n [9, 7, 1]])\r\nprint(arr_2d)\r\nflat_array = arr_2d.flatten()\r\nflat_array[2] = 50\r\nprint('Flattened 1D Numpy Array:')\r\nprint(flat_array)\r\nprint('Original 2D Numpy Array')\r\nprint(arr_2d)\r\n<\/pre>\n
[[7 4 2]\r\n[5 4 3]\r\n[9 7 1]]\r\n\r\nFlattened 1D Numpy Array:\r\n[ 7 4 50 5 4 3 9 7 1]\r\n\r\nOriginal 2D Numpy Array\r\n[[7 4 2]\r\n[5 4 3]\r\n[9 7 1]]\r\n\r\n<\/pre>\n
<\/a>Flatten a 2D Numpy Array along Different Axis using flatten()<\/h2>\n
\nIt tells the order.<\/p>\n\n
<\/a>Flatten 2D array row-wise<\/h3>\n
import numpy as np\r\n\r\n# Create a 2D Numpy array from list of list\r\narr_2d = np.array([[7, 4, 2],\r\n [5, 4, 3],\r\n [9, 7, 1]])\r\nflat_array = arr_2d.flatten(order='C')\r\nprint('Flattened 1D Numpy Array:')\r\nprint(flat_array)<\/pre>\n
Flattened 1D Numpy Array:\r\n[7 4 2 5 4 3 9 7 1]\r\n<\/pre>\n
<\/a>Flatten 2D array column-wise<\/h3>\n
import numpy as np\r\n\r\n# Create a 2D Numpy array from list of list\r\narr_2d = np.array([[7, 4, 2],\r\n [5, 4, 3],\r\n [9, 7, 1]])\r\nflat_array = arr_2d.flatten(order='F')\r\nprint('Flattened 1D Numpy Array:')\r\nprint(flat_array)<\/pre>\n
Flattened 1D Numpy Array:\r\n[7 5 9 4 4 7 2 3 1]\r\n<\/pre>\n
<\/a>Flatten 2D array based on memory layout<\/h3>\n
import numpy as np\r\n\r\n# Create a 2D Numpy array from list of list\r\narr_2d = np.array([[7, 4, 2],\r\n [5, 4, 3],\r\n [9, 7, 1]])\r\n# Create a transpose view of array\r\ntrans_arr = arr_2d.T\r\nprint('Transpose view of array:')\r\nprint(trans_arr)<\/pre>\n
Transpose view of array:\r\n[[7 5 9]\r\n[4 4 7]\r\n[2 3 1]]\r\n<\/pre>\n
import numpy as np\r\n\r\n# Create a 2D Numpy array from list of list\r\narr_2d = np.array([[7, 4, 2],\r\n [5, 4, 3],\r\n [9, 7, 1]])\r\n# Create a transpose view of array\r\ntrans_arr = arr_2d.T\r\nflat_array = trans_arr.flatten(order='C')\r\nprint(flat_array )<\/pre>\n
[7 5 9 4 4 7 2 3 1]\r\n<\/pre>\n
<\/a>Flatten a 3D array to a 1D numpy array using ndarray.flatten()<\/h2>\n
import numpy as np\r\n\r\n# Create a 3D Numpy array\r\narr = np.arange(12).reshape((2,3,2))\r\nprint('3D Numpy array:')\r\nprint(arr)<\/pre>\n
3D Numpy array:\r\n[[[ 0 1]\r\n[ 2 3]\r\n[ 4 5]]\r\n\r\n[[ 6 7]\r\n[ 8 9]\r\n[10 11]]]\r\n<\/pre>\n
import numpy as np\r\n\r\n# Create a 3D Numpy array\r\narr = np.arange(12).reshape((2,3,2))\r\n# Convert 3D array to 1D\r\nflat_array = arr.flatten()\r\nprint('Flattened 1D Numpy Array:')\r\nprint(flat_array)<\/pre>\n
Flattened 1D Numpy Array:\r\n[ 0 1 2 3 4 5 6 7 8 9 10 11]\r\n<\/pre>\n
<\/a>Flatten a list of arrays using numpy.ndarray.flatten()<\/h2>\n
# Create a list of numpy arrays\r\narr = np.arange(5)\r\nlist_of_arr = [arr] * 5\r\nprint('Iterate over the list of a numpy array')\r\nfor elem in list_of_arr:\r\n print(elem)<\/pre>\n
Iterate over the list of a numpy array\r\n[0 1 2 3 4]\r\n[0 1 2 3 4]\r\n[0 1 2 3 4]\r\n[0 1 2 3 4]\r\n[0 1 2 3 4]<\/pre>\n
# Convert a list of numpy arrays to a flat array\r\nflat_array = np.array(list_of_arr).flatten()\r\nprint('Flattened 1D Numpy Array:')\r\nprint(flat_array)<\/pre>\n
Flattened 1D Numpy Array:\r\n[0 1 2 3 4 0 1 2 3 4 0 1 2 3 4 0 1 2 3 4 0 1 2 3 4]<\/pre>\n
<\/a>Flatten a list of lists using ndarray.flatten()<\/h2>\n
# Create a list of list\r\nlist_of_lists = [[1, 2, 3, 4, 5],\r\n [1, 2, 3, 4, 5],\r\n [1, 2, 3, 4, 5],\r\n [1, 2, 3, 4, 5]]\r\n# Create a 2D numpy array from a list of list and flatten that array\r\nflat_array = np.array(list_of_lists).flatten()\r\nprint('Flattened 1D Numpy Array:')\r\nprint(flat_array)\r\n# Convert the array to list\r\nprint('Flat List:')\r\nprint(list(flat_array))<\/pre>\n
Flattened 1D Numpy Array:\r\n[1 2 3 4 5 1 2 3 4 5 1 2 3 4 5 1 2 3 4 5]\r\nFlat List:\r\n[1, 2, 3, 4, 5, 1, 2, 3, 4, 5, 1, 2, 3, 4, 5, 1, 2, 3, 4, 5]<\/pre>\n
Conclusion:<\/h3>\n