{"id":7056,"date":"2023-11-01T15:27:08","date_gmt":"2023-11-01T09:57:08","guid":{"rendered":"https:\/\/python-programs.com\/?p=7056"},"modified":"2023-11-10T12:11:43","modified_gmt":"2023-11-10T06:41:43","slug":"python-numpy-reshape-function-tutorial-with-examples","status":"publish","type":"post","link":"https:\/\/python-programs.com\/python-numpy-reshape-function-tutorial-with-examples\/","title":{"rendered":"Python: numpy.reshape() function Tutorial with examples"},"content":{"rendered":"
In this article we will see how we can use numpy.reshape() function to change the shape of a numpy array.<\/p>\n
Syntax<\/u>:- numpy.reshape(a, newshape, order='C')<\/pre>\nwhere,<\/p>\n
To pass 1D numpy array to 2D numpy array we will pass array and tuple i.e. (3×3) as numpy to The new shape formed must be compatible with the shape of array passed i.e. if rows denoted by ‘R’, columns by ‘C’, total no. of items by ‘N’ then new shape must satisfy the relation R*C=N <\/em>otherwise it will give rise to error.<\/p>\n We can convert a 1D numpy array into 3D numpy array passing array and shape of 3D array as tuple to reshape() function.<\/p>\n We can also even convert a 3D numpy array to 2D numpy array.<\/p>\n If we pass a numpy array and ‘-1’ to reshape() then it will get convert into array of any shape to a flat array.<\/p>\n If possible in some scenarios reshape() function returns a view of the passed object. If we modify anything in view object it will reflect in main objet and vice-versa.<\/p>\n In some scenarios reshape() function may not return a view object. We can check what object reshape() returns by seeing its base attribute if it is view or not.<\/p>\n If base attribute is None <\/em>then it is not a view object, else it is a view object i.e. base attribute point to original array object.<\/p>\n We can also pass order parameter whose value can be ‘C’ or ‘F’ or ‘A’. This parameter will decide in which order the elements of given array will be used. Default value of order parameter is ‘C’.<\/p>\n \u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 <\/strong>By passing order paramter ‘C’ in reshape() function the given array will be read row wise.<\/p>\n \u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 <\/strong>By passing order parameter ‘C’ in reshape() function the given array will be read row wise.<\/p>\n \u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 <\/strong>If we pass order as ‘A’ in reshape() function, then items of input array will be read\u00a0 basis on internal memory unit.<\/p>\n Here it will read elements based on memory layout of original given array and it does not consider the current view of input array<\/p>\n <\/p>\n <\/p>\n","protected":false},"excerpt":{"rendered":" Understanding numpy.reshape() function Tutorial with examples In this article we will see how we can use numpy.reshape() function to change the shape of a numpy array. numpy.reshape() : Syntax:- numpy.reshape(a, newshape, order=’C’) where, a :\u00a0Array, list or list of lists which need to be reshaped. newshape :\u00a0New shape which is a tuple or a int. …<\/p>\nreshape()<\/code> function.<\/p>\n
import numpy as sc\r\n# Produce a 1D Numpy array from a given list\r\nnumArr = sc.array([10, 20, 30, 40, 50, 60, 70, 80, 92])\r\nprint('Original Numpy array:')\r\nprint(numArr)\r\n# Convert the 1D Numpy array to a 2D Numpy array\r\narr_twoD = sc.reshape(numArr, (3,3))\r\nprint('2D Numpy array:')\r\nprint(arr_twoD)\r\n<\/pre>\n
Output :\r\nOriginal Numpy array:\r\n[10 20 30 40 50 60 70 80 92]\r\n2D Numpy array:\r\n[[10 20 30]\r\n [40 50 60]\r\n\u00a0[70 80 90]]<\/pre>\n
New shape must be compatible with the original shape :<\/h3>\n
import numpy as sc\r\n# Produce a 1D Numpy array from a given list\r\nnumArr = sc.array([10, 20, 30, 40, 50, 60, 70, 80, 92])\r\nprint('Original Numpy array:')\r\nprint(numArr)\r\n# convert the 1D Numpy array to a 2D Numpy array\r\narr_twoD = sc.reshape(numArr, (3,2))\r\nprint('2D Numpy array:')\r\nprint(arr_twoD)\r\n<\/pre>\n
Output :\r\nValueError: total size of new array must be unchanged\r\n\r\n<\/pre>\n
Using numpy.reshape() to convert a 1D numpy array to a 3D Numpy array :<\/strong><\/h4>\n
import numpy as sc\r\n# Produce a 1D Numpy array from a given list\r\nnumArr = sc.array([10, 20, 30, 40, 50, 60, 70, 80, 90, 91, 95, 99])\r\nprint('Original Numpy array:')\r\nprint(numArr)\r\n# Convert the 1D Numpy array to a 3D Numpy array\r\narr_threeD = sc.reshape(numArr, (3,2,2))\r\nprint('3D Numpy array:')\r\nprint(arr_threeD)\r\n<\/pre>\n
Output :\r\nOriginal Numpy array:\r\n[10 20 30 40 50 60 70 80 90 91 95 99]\r\n3D Numpy array:\r\n[[[10 20]\r\n [30 40]]\r\n [[50 60]\r\n [70 80]]\r\n [[90 91]\r\n\u00a0 [95 99]]]<\/pre>\n
Use numpy.reshape() to convert a 3D numpy array to a 2D Numpy array :<\/h3>\n
import numpy as sc\r\n# Create a 3D numpy array\r\narr_threeD = sc.array([[[10, 20],\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 [30, 40],\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 [50, 60]],\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 [[70, 80],\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 [90, 91],\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 [95, 99]]])\r\nprint('3D Numpy array:')\r\nprint(arr_threeD)\r\n# Converting 3D numpy array to numpy array of size 2x6\r\narr_twoD = sc.reshape(arr_threeD, (2,6))\r\nprint('2D Numpy Array:')\r\nprint(arr_twoD)<\/pre>\n
Output :\r\n3D Numpy array:\r\n[[[10 20]\r\n [30 40]\r\n [50 60]]\r\n [[70 80]\r\n [90 91]\r\n [95 99]]]\r\n2D Numpy Array:\r\n[[10 20 30 40 50 60]\r\n\u00a0[70 80 90 91 95 99]]<\/pre>\n
Use numpy.reshape() to convert a 2D numpy array to a 1D Numpy array :<\/h3>\n
import numpy as sc\r\narr_twoD = sc.array([[10, 20, 30],\r\n [30, 40, 50],\r\n [60, 70, 82]]) \r\n# Covert numpy array of any shape to 1D array\r\nflatn_arr = sc.reshape(arr_twoD, -1)\r\nprint('1D Numpy array:')\r\nprint(flatn_arr) \r\n<\/pre>\n
Output :\r\n1D Numpy array:\r\n[10 20 30 30 40 50 60 70 82]<\/pre>\n
numpy.reshape() returns a new view object if possible :<\/h3>\n
import numpy as sc\r\n# create a 1D Numpy array\r\nnum_arr = sc.array([10, 20, 30, 40, 50, 60, 70, 80, 92]) \r\n# Get a View object of any shape \r\narr_twoD = sc.reshape(num_arr, (3,3))\r\nprint('Original array:')\r\nprint(arr_twoD)\r\n# Modify the 5th element of the original array \r\n# Modification will also be visible in view object\r\nnum_arr[4] = 9\r\nprint('Modified 1D Numpy array:')\r\nprint(num_arr)\r\nprint('2D Numpy array:')\r\nprint(arr_twoD)\r\n<\/pre>\n
Output :\r\nOriginal array:\r\n[[10 20 30]\r\n [40 50 60]\r\n [70 80 92]]\r\nModified 1D Numpy array:\r\n[10 20 30 40\u00a0 9 60 70 80 90]\r\n2D Numpy array:\r\n[[10 20 30]\r\n [40\u00a0 9 60]\r\n\u00a0[70 80 90]]<\/pre>\n
How to check if reshape() returned a view object ?<\/h3>\n
import numpy as sc\r\n# create a 1D Numpy array\r\nnum_arr = sc.array([10, 20, 30, 40, 50, 60, 70, 80, 90])\r\narr_twoD = sc.reshape(num_arr, (3,3))\r\nif arr_twoD.base is not None:\r\n print('arr_twoD is a view of original array')\r\n print('base array : ', arr_twoD.base)\r\n<\/pre>\n
Output :\r\narr_twoD is a view of original array\r\nbase array :\u00a0 [10 20 30 40 50 60 70 80 90]<\/pre>\n
numpy.reshape() & different type of order parameters :<\/h3>\n
Convert 1D to 2D array row wise with order \u2018C\u2019 :<\/strong><\/h3>\n
import numpy as sc\r\n# create a 1D Numpy array\r\nnum_arr = sc.array([10, 20, 30, 40, 50, 60, 70, 80, 92])\r\nprint('original array:')\r\nprint(num_arr)\r\n# Covert 1D numpy array to 2D by reading array in row wise manner\r\narr_twoD = sc.reshape(num_arr, (3, 3), order = 'C')\r\nprint('2D array being read in row wise manner:')\r\nprint(arr_twoD)\r\n<\/pre>\n
Output :\r\noriginal array:\r\n[10 20 30 40 50 60 70 80 92]\r\n2D array being read in row wise manner:\r\n[[10 20 30]\r\n [40 50 60]\r\n\u00a0[70 80 90]]<\/pre>\n
Convert 1D to 2D array column wise with order \u2018F\u2019 :<\/h4>\n
import numpy as sc\r\n# create a 1D Numpy array\r\nnum_arr = sc.array([10, 20, 30, 40, 50, 60, 70, 80, 92])\r\nprint('original array:')\r\nprint(num_arr)\r\n# Covert 1D numpy array to 2D by reading array in column wise manner\r\narr_twoD = sc.reshape(num_arr, (3, 3), order = 'F')\r\nprint('2D array being read in column wise manner:')\r\nprint(arr_twoD)\r\n<\/pre>\n
Output :\r\noriginal array:\r\n[10 20 30 40 50 60 70 80 92]\r\n2D array being read in column wise manner:\r\n[[10 40 70]\r\n [20 50 80]\r\n\u00a0[30 60 90]]<\/pre>\n
Convert 1D to 2D array by memory layout with parameter order \u201cA\u201d :<\/h4>\n
import numpy as sc\r\n# create a 1D Numpy array\r\nnum_arr = sc.array([10, 20, 30, 40, 50, 60, 70, 80, 90])\r\nprint('Original 1D array: ',num_arr)\r\n# Create a 2D view object and get transpose view of it\r\narr_twoD = sc.reshape(num_arr, (3, 3)).T\r\nprint('2D transposed View:')\r\nprint(arr_twoD)\r\n# Read elements in row wise from memory layout of original 1D array\r\nflatn_arr = sc.reshape(arr_twoD, 9, order='A')\r\nprint('Flattened 1D array')\r\nprint(flatn_arr)\r\n<\/pre>\n
Output :\r\nOriginal 1D array:\u00a0 [10 20 30 40 50 60 70 80 90]\r\n2D transposed View:\r\n[[10 40 70]\r\n [20 50 80]\r\n [30 60 90]]\r\nFlattened 1D array\r\n[10 20 30 40 50 60 70 80 90]<\/pre>\n
Convert the shape of a list using numpy.reshape() :<\/h3>\n
\n
import numpy as sc\r\nnum_list = [10,20,30,40,50,60,70,80,90]\r\n# To convert a list to 2D Numpy array\r\narr_twoD = sc.reshape(num_list, (3,3))\r\nprint('2D Numpy array:')\r\nprint(arr_twoD)\r\n<\/pre>\n
Output :\r\n2D Numpy array:\r\n[[10 20 30]\r\n [40 50 60]\r\n\u00a0[70 80 90]]<\/pre>\n
\n
import numpy as sc\r\nnum_list = [10,20,30,40,50,60,70,80,90]\r\n# Convert a given list to 2D Numpy array\r\narr_twoD = sc.reshape(num_list, (3,3))\r\nprint('2D Numpy array:')\r\nprint(arr_twoD)\r\n# Convert the 2D Numpy array to list of list\r\nlist_list = [ list(elem) for elem in arr_twoD]\r\nprint('List of List: ')\r\nprint(list_list)\r\n<\/pre>\n
Output :\r\n2D Numpy array:\r\n[[10 20 30]\r\n [40 50 60]\r\n [70 80 90]]\r\nList of List:\r\n[[10, 20, 30], [40, 50, 60], [70, 80, 90]]<\/pre>\n