Numpy.reshape – Python: numpy.reshape() function Tutorial with examples

Understanding numpy.reshape() function Tutorial with examples

Numpy.reshape: 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 : Array, list or list of lists which need to be reshaped.
  • newshape : New shape which is a tuple or a int. (Pass tuple for converting a 2D or 3D array and Pass integer for creating array of 1D shape.)
  • order : Order in which items from given array will be used. (‘C‘: Read items from array in row-wise manner, ‘F‘: Read items from array in column-wise manner, ‘A‘: Read items from array based on memory order of items)

Converting shapes of Numpy arrays using numpy.reshape() :

Use numpy.reshape() to convert a 1D numpy array to a 2D Numpy array :

To pass 1D numpy array to 2D numpy array we will pass array and tuple i.e. (3×3) as numpy to reshape() function.

import numpy as sc
# Produce a 1D Numpy array from a given list
numArr = sc.array([10, 20, 30, 40, 50, 60, 70, 80, 92])
print('Original Numpy array:')
print(numArr)
# Convert the 1D Numpy array to a 2D Numpy array
arr_twoD = sc.reshape(numArr, (3,3))
print('2D Numpy array:')
print(arr_twoD)
Output :
Original Numpy array:
[10 20 30 40 50 60 70 80 92]
2D Numpy array:
[[10 20 30]
 [40 50 60]
 [70 80 90]]

New shape must be compatible with the original shape :

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 otherwise it will give rise to error.

import numpy as sc
# Produce a 1D Numpy array from a given list
numArr = sc.array([10, 20, 30, 40, 50, 60, 70, 80, 92])
print('Original Numpy array:')
print(numArr)
# convert the 1D Numpy array to a 2D Numpy array
arr_twoD = sc.reshape(numArr, (3,2))
print('2D Numpy array:')
print(arr_twoD)
Output :
ValueError: total size of new array must be unchanged

Using numpy.reshape() to convert a 1D numpy array to a 3D Numpy array :

We can convert a 1D numpy array into 3D numpy array passing array and shape of 3D array as tuple to reshape() function.

import numpy as sc
# Produce a 1D Numpy array from a given list
numArr = sc.array([10, 20, 30, 40, 50, 60, 70, 80, 90, 91, 95, 99])
print('Original Numpy array:')
print(numArr)
# Convert the 1D Numpy array to a 3D Numpy array
arr_threeD = sc.reshape(numArr, (3,2,2))
print('3D Numpy array:')
print(arr_threeD)
Output :
Original Numpy array:
[10 20 30 40 50 60 70 80 90 91 95 99]
3D Numpy array:
[[[10 20]
  [30 40]]
 [[50 60]
  [70 80]]
 [[90 91]
  [95 99]]]

Use numpy.reshape() to convert a 3D numpy array to a 2D Numpy array :

We can also even convert a 3D numpy array to 2D numpy array.

import numpy as sc
# Create a 3D numpy array
arr_threeD = sc.array([[[10, 20],
                   [30, 40],
                   [50, 60]],
                 [[70, 80],
                  [90, 91],
                  [95, 99]]])
print('3D Numpy array:')
print(arr_threeD)
# Converting 3D numpy array to numpy array of size 2x6
arr_twoD = sc.reshape(arr_threeD, (2,6))
print('2D Numpy Array:')
print(arr_twoD)
Output :
3D Numpy array:
[[[10 20]
  [30 40]
  [50 60]]
 [[70 80]
  [90 91]
  [95 99]]]
2D Numpy Array:
[[10 20 30 40 50 60]
 [70 80 90 91 95 99]]

Use numpy.reshape() to convert a 2D numpy array to a 1D Numpy array :

If we pass a numpy array and ‘-1’ to reshape() then it will get convert into array of any shape to a flat array.

import numpy as sc
arr_twoD = sc.array([[10, 20, 30],
                [30, 40, 50],
                [60, 70, 82]])     
# Covert numpy array of any shape to 1D array
flatn_arr = sc.reshape(arr_twoD, -1)
print('1D Numpy array:')
print(flatn_arr) 
Output :
1D Numpy array:
[10 20 30 30 40 50 60 70 82]

numpy.reshape() returns a new view object if possible :

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.

import numpy as sc
# create a 1D Numpy array
num_arr = sc.array([10, 20, 30, 40, 50, 60, 70, 80, 92])  
# Get a View object of any shape 
arr_twoD = sc.reshape(num_arr, (3,3))
print('Original array:')
print(arr_twoD)
# Modify the 5th element of the original array 
# Modification will also be visible in view object
num_arr[4] = 9
print('Modified 1D Numpy array:')
print(num_arr)
print('2D Numpy array:')
print(arr_twoD)
Output :
Original array:
[[10 20 30]
 [40 50 60]
 [70 80 92]]
Modified 1D Numpy array:
[10 20 30 40  9 60 70 80 90]
2D Numpy array:
[[10 20 30]
 [40  9 60]
 [70 80 90]]

How to check if reshape() returned a view object ?

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.

If base attribute is None then it is not a view object, else it is a view object i.e. base attribute point to original array object.

import numpy as sc
# create a 1D Numpy array
num_arr = sc.array([10, 20, 30, 40, 50, 60, 70, 80, 90])
arr_twoD = sc.reshape(num_arr, (3,3))
if arr_twoD.base is not None:
    print('arr_twoD is a view of original array')
    print('base array : ', arr_twoD.base)
Output :
arr_twoD is a view of original array
base array :  [10 20 30 40 50 60 70 80 90]

numpy.reshape() & different type of order parameters :

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’.

Convert 1D to 2D array row wise with order ‘C’ :

                By passing order paramter ‘C’ in reshape() function the given array will be read row wise.

import numpy as sc
# create a 1D Numpy array
num_arr = sc.array([10, 20, 30, 40, 50, 60, 70, 80, 92])
print('original array:')
print(num_arr)
# Covert 1D numpy array to 2D by reading array in row wise manner
arr_twoD = sc.reshape(num_arr, (3, 3), order = 'C')
print('2D array being read in row wise manner:')
print(arr_twoD)
Output :
original array:
[10 20 30 40 50 60 70 80 92]
2D array being read in row wise manner:
[[10 20 30]
 [40 50 60]
 [70 80 90]]

Convert 1D to 2D array column wise with order ‘F’ :

                By passing order parameter ‘C’ in reshape() function the given array will be read row wise.

import numpy as sc
# create a 1D Numpy array
num_arr = sc.array([10, 20, 30, 40, 50, 60, 70, 80, 92])
print('original array:')
print(num_arr)
# Covert 1D numpy array to 2D by reading array in column wise manner
arr_twoD = sc.reshape(num_arr, (3, 3), order = 'F')
print('2D array being read in column wise manner:')
print(arr_twoD)
Output :
original array:
[10 20 30 40 50 60 70 80 92]
2D array being read in column wise manner:
[[10 40 70]
 [20 50 80]
 [30 60 90]]

Convert 1D to 2D array by memory layout with parameter order “A” :

                If we pass order as ‘A’ in reshape() function, then items of input array will be read  basis on internal memory unit.

Here it will read elements based on memory layout of original given array and it does not consider the current view of input array

import numpy as sc
# create a 1D Numpy array
num_arr = sc.array([10, 20, 30, 40, 50, 60, 70, 80, 90])
print('Original 1D array: ',num_arr)
# Create a 2D view object and get transpose view of it
arr_twoD = sc.reshape(num_arr, (3, 3)).T
print('2D transposed View:')
print(arr_twoD)
# Read elements in row wise from memory layout of original 1D array
flatn_arr = sc.reshape(arr_twoD, 9, order='A')
print('Flattened 1D array')
print(flatn_arr)
Output :
Original 1D array:  [10 20 30 40 50 60 70 80 90]
2D transposed View:
[[10 40 70]
 [20 50 80]
 [30 60 90]]
Flattened 1D array
[10 20 30 40 50 60 70 80 90]

Convert the shape of a list using numpy.reshape() :

  • In reshape() function we can pass list or list of list instead of array.
import numpy as sc
num_list = [10,20,30,40,50,60,70,80,90]
# To convert a list to 2D Numpy array
arr_twoD = sc.reshape(num_list, (3,3))
print('2D Numpy array:')
print(arr_twoD)
Output :
2D Numpy array:
[[10 20 30]
 [40 50 60]
 [70 80 90]]
  • We can also convert a 2D numpy array to list of list
import numpy as sc
num_list = [10,20,30,40,50,60,70,80,90]
# Convert a given list to 2D Numpy array
arr_twoD = sc.reshape(num_list, (3,3))
print('2D Numpy array:')
print(arr_twoD)
# Convert the 2D Numpy array to list of list
list_list = [ list(elem) for elem in arr_twoD]
print('List of List: ')
print(list_list)
Output :
2D Numpy array:
[[10 20 30]
 [40 50 60]
 [70 80 90]]
List of List:
[[10, 20, 30], [40, 50, 60], [70, 80, 90]]