{"id":5671,"date":"2023-10-28T13:28:49","date_gmt":"2023-10-28T07:58:49","guid":{"rendered":"https:\/\/python-programs.com\/?p=5671"},"modified":"2023-11-10T12:04:39","modified_gmt":"2023-11-10T06:34:39","slug":"python-convert-matrix-2d-numpy-array-to-a-1d-numpy-array","status":"publish","type":"post","link":"https:\/\/python-programs.com\/python-convert-matrix-2d-numpy-array-to-a-1d-numpy-array\/","title":{"rendered":"Python: Convert Matrix \/ 2D Numpy Array to a 1D Numpy Array | How to make a 2d Array into a 1d Array in Python?"},"content":{"rendered":"
This article is all about converting 2D Numpy Array to a 1D Numpy Array. Changing a 2D NumPy array into a 1D array returns in an array containing the same elements as the original, but with only one row. Want to learn how to convert 2d Array into 1d Array using Python? Then, stay tuned to this tutorial and jump into the main heads via the available links shown below:<\/p>\n
Python Numpy provides a function flatten() to convert an array of any shape to a flat 1D array.<\/p>\n
Firstly, it is required to import the numpy module,<\/p>\n
Syntax:<\/p>\n Order: In which items from the array will be read<\/p>\n Order=’C’: It will read items from array row-wise<\/p>\n Order=’F’: It will read items from array row-wise<\/p>\n Order=’A’: It will read items from array-based on memory order<\/p>\n Suppose we have a 2D Numpy array or matrix,<\/p>\n [7 4 2] Which we have to convert in a 1D array. Let’s use this to convert a 2D numpy array or matrix to a new flat 1D numpy array,<\/p>\n Output:<\/strong><\/p>\n If we made any changes in our 1D array it will not affect our original 2D array.<\/p>\n Output:<\/strong><\/p>\n Also Check:<\/p>\n Numpy have\u00a0 a built-in function ‘numpy.ravel()’ that accepts an array element as parameter and returns a flatten 1D array.<\/p>\n Syntax:<\/strong><\/p>\n Let’s make use of this syntax to convert 2D array to 1D array,<\/p>\n Output:<\/strong><\/p>\n If we made any changes in our 1D array using numpy.ravel() it will also affect our original 2D array.<\/p>\n Output:<\/strong><\/p>\n Numpy provides a built-in function reshape() to convert the shape of a numpy array,<\/p>\n It accepts three arguments-<\/p>\n Output:<\/strong><\/p>\n In the above example, we have pass 9 as an argument because there were a total of 9 elements (3X3) in the 2D input array.<\/p>\n This function can be used when the input array is too big and multidimensional or we just don\u2019t know the total elements in the array. In such scenarios, we can pass the size as -1.<\/p>\n Output:<\/strong><\/p>\n With the help of reshape() function, we can view the input array and any modification done in the view object will be reflected in the original input too.<\/p>\n Output:<\/strong><\/p>\n If we pass the order parameter in reshape() function as \u201cF\u201d then it will read 2D input array column-wise. As we will show below-<\/p>\n Output:<\/strong><\/p>\n This article is all about converting 2D Numpy Array to a 1D Numpy Array. Changing a 2D NumPy array into a 1D array returns in an array containing the same elements as the original, but with only one row. Want to learn how to convert 2d Array into 1d Array using Python? Then, stay tuned …<\/p>\nimport numpy as np<\/code><\/p>\n
ndarray.flatten(order='C')\r\nndarray.flatten(order='F')\r\nndarray.flatten(order='A')<\/pre>\n
\n[5 3 6]
\n[2 9 5]<\/p>\nimport numpy as np\r\n# Create a 2D numpy array from list of lists\r\narr = np.array([[7, 4, 2],\r\n [5, 3, 6],\r\n [2, 9, 5]])\r\n# get a flatten 1D copy of 2D Numpy array\r\nflat_array = arr.flatten()\r\nprint('1D Numpy Array:')\r\nprint(flat_array)<\/pre>\n
1D Numpy Array:\r\n[7 4 2 5 3 6 2 9 5]\r\n<\/pre>\n
import numpy as np\r\n# Create a 2D numpy array from list of lists\r\narr = np.array([[7, 4, 2],\r\n [5, 3, 6],\r\n [2, 9, 5]])\r\n# get a flatten 1D copy of 2D Numpy array\r\nflat_array = arr.flatten()\r\nprint('1D Numpy Array:')\r\nprint(flat_array)\r\n# Modify the flat 1D array\r\nflat_array[0] = 50\r\nprint('Modified Flat Array: ')\r\nprint(flat_array)\r\nprint('Original Input Array: ')\r\nprint(arr)<\/pre>\n
1D Numpy Array:\r\n[7 4 2 5 3 6 2 9 5]\r\n\r\nModified Flat Array:\r\n[50 4 2 5 3 6 2 9 5]\r\n\r\nOriginal Input Array:\r\n[[7 4 2]\r\n[5 3 6]\r\n[2 9 5]]\r\n<\/pre>\n
\n
<\/a>Convert 2D Numpy array to 1D Numpy array using numpy.ravel()<\/h2>\n
numpy.ravel(input_arr, order='C')<\/code><\/p>\n
import numpy as np\r\n# Create a 2D numpy array from list of lists\r\narr = np.array([[7, 4, 2],\r\n [5, 3, 6],\r\n [2, 9, 5]])\r\n# Get a flattened view of 2D Numpy array\r\nflat_array = np.ravel(arr)\r\nprint('Flattened 1D Numpy array:')\r\nprint(flat_array)\r\n<\/pre>\n
Flattened 1D Numpy array:\r\n[7 4 2 5 3 6 2 9 5]\r\n\r\n<\/pre>\n
import numpy as np\r\n# Create a 2D numpy array from list of lists\r\narr = np.array([[7, 4, 2],\r\n [5, 3, 6],\r\n [2, 9, 5]])\r\n# Get a flattened view of 2D Numpy array\r\nflat_array = np.ravel(arr)\r\nprint('Flattened 1D Numpy array:')\r\nprint(flat_array)\r\n# Modify the 2nd element in flat array\r\nflat_array[1] = 12\r\n# Changes will be reflected in both flat array and original 2D array\r\nprint('Modified Flattened 1D Numpy array:')\r\nprint(flat_array)\r\nprint('2D Numpy Array:')\r\nprint(arr)\r\n<\/pre>\n
Flattened 1D Numpy array:\r\n[7 4 2 5 3 6 2 9 5]\r\nModified Flattened 1D Numpy array:\r\n[ 7 12 2 5 3 6 2 9 5]\r\n2D Numpy Array:\r\n[[ 7 12 2]\r\n[ 5 3 6]\r\n[ 2 9 5]]\r\n\r\n<\/pre>\n
<\/a>Convert a 2D Numpy array to a 1D array using numpy.reshape()<\/h2>\n
\n
import numpy as np\r\n# Create a 2D numpy array from list of lists\r\narr = np.array([[7, 4, 2],\r\n [5, 3, 6],\r\n [2, 9, 5]])\r\n\r\n# convert 2D array to a 1D array of size 9\r\nflat_arr = np.reshape(arr, 9)\r\nprint('1D Numpy Array:')\r\nprint(flat_arr)\r\n<\/pre>\n
1D Numpy Array:\r\n[7 4 2 5 3 6 2 9 5]\r\n\r\n<\/pre>\n
<\/a>numpy.reshape() and -1 size<\/h3>\n
import numpy as np\r\n# Create a 2D numpy array from list of lists\r\narr = np.array([[7, 4, 2],\r\n [5, 3, 6],\r\n [2, 9, 5]])\r\n\r\n# convert 2D array to a 1D array without mentioning the actual size\r\nflat_arr = np.reshape(arr, -1)\r\nprint('1D Numpy Array:')\r\nprint(flat_arr)<\/pre>\n
1D Numpy Array:\r\n[7 4 2 5 3 6 2 9 5]\r\n\r\n<\/pre>\n
<\/a>numpy.reshape() returns a new view object if possible<\/h3>\n
import numpy as np\r\n# Create a 2D numpy array from list of lists\r\narr = np.array([[7, 4, 2],\r\n[5, 3, 6],\r\n[2, 9, 5]])\r\nflat_arr = np.reshape(arr,-1)\r\nprint('1D Numpy Array:')\r\nprint(flat_arr)\r\n# Modify the element at the first row and first column in the 1D array\r\narr[0][0] = 11\r\nprint('1D Numpy Array:')\r\nprint(flat_arr)\r\nprint('2D Numpy Array:')\r\nprint(arr)<\/pre>\n
1D Numpy Array:\r\n[7 4 2 5 3 6 2 9 5]\r\n\r\n1D Numpy Array:\r\n[11 4 2 5 3 6 2 9 5]\r\n\r\n2D Numpy Array:\r\n\r\n[[11 4 2]\r\n[ 5 3 6]\r\n[ 2 9 5]]\r\n\r\n<\/pre>\n
<\/a>Convert 2D Numpy array to 1D array but Column Wise<\/h3>\n
import numpy as np\r\n# Create a 2D numpy array from list of lists\r\narr = np.array([[7, 4, 2],\r\n [5, 3, 6],\r\n [2, 9, 5]])\r\n# Read 2D array column by column and create 1D array from it\r\nflat_arr = np.reshape(arr, -1, order='F')\r\nprint('1D Numpy Array:')\r\nprint(flat_arr)\r\n<\/pre>\n
1D Numpy Array:\r\n[7 5 2 4 3 9 2 6 5]\r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"