While working with arrays many times we come across situations where we need to change the shape of that array but it is a very time-consuming process because first, we copy the data and then arrange it into the desired shape, but in Python, we have a function called reshape() for this purpose.
What is numpy.reshape() in Python
The numpy.reshape() function shapes an array without changing the data of the array.
Syntax: numpy.reshape(array, shape, order)
Here we will see the use of reshape() function in Python.
Python3
import numpy as np array1 = np.arange( 8 ) print ( "Original array : \n" , array1) # shape array with 2 rows and 4 columns array2 = np.arange( 8 ).reshape( 2 , 4 ) print ( "\narray reshaped with 2 rows and 4 columns : \n" , array2) # shape array with 4 rows and 2 columns array3 = np.arange( 8 ).reshape( 4 , 2 ) print ( "\narray reshaped with 4 rows and 2 columns : \n" , array3) |
Output:
Original array : [0 1 2 3 4 5 6 7]
array reshaped with 2 rows and 4 columns : [[0 1 2 3] [4 5 6 7]]
array reshaped with 4 rows and 2 columns : [[0 1] [2 3] [4 5] [6 7]]
Finding Unknown Dimensions in Numpy
When we are unaware of any dimension of the array then we are allowed to have only one “unknown” dimension. This represents that we do not have to specify any number for one of that dimensions in the reshape method. We pass -1 as the value for the unknown dimension, and NumPy will calculate this unknown.
Example 1
Convert a Numpy array with 8 elements to a 3D array with 2×2 elements.
Python3
import numpy as np arr = np.array([ 1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 ]) newarr = arr.reshape( 2 , 2 , - 1 ) print (newarr) |
Output:
[[[1 2] [3 4]] [[5 6] [7 8]]]
Example 2
Convert a Numpy array with 8 elements to a 3D array with 4×4 elements.
Python3
import numpy as np arr = np.array([ 1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 ]) newarr = arr.reshape( 2 , - 1 , 4 ) print (newarr) |
Output:
[[[1 2 3 4]] [[5 6 7 8]]]
Example 3
Convert a Numpy array with 8 elements to a 3D array with 4×2 elements.
Python3
import numpy as np arr = np.array([ 1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 ]) newarr = arr.reshape( - 1 , 4 , 2 ) print (newarr) |
Output:
[[[1 2] [3 4] [5 6] [7 8]]]
Example 4
Try using -1 for more than one unknown dimension.
Python3
import numpy as np arr = np.array([ 1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 ]) newarr = arr.reshape( 2 , - 1 , - 1 ) print (newarr) |
Output:
Hence, we can see that we can not pass -1 to more than one.
Traceback (most recent call last):
File “main.py”, line 3, in <module>
newarr = arr.reshape(2, -1, -1)
ValueError: can only specify one unknown dimension
Flattening the Numpy arrays
Flattening an array means converting an array to a 1D array. We can use reshape(-1) to do this.
Example 1
Convert a 3D array into a 1D array
Python3
import numpy as np a = np.array([[ 'G' , 'F' , 'G' ], [ 'G' , 'F' , 'G' ], [ 'G' , 'F' , 'G' ]]) na = a.reshape( - 1 ) print (na) |
Output:
[‘G’ ‘F’ ‘G’ ‘G’ ‘F’ ‘G’ ‘G’ ‘F’ ‘G’]
Example 2
Convert the 2D array into a 1D array:
Python3
import numpy as np arr = np.array([[ 1 , 2 , 3 ], [ 4 , 5 , 6 ]]) newarr = arr.reshape( - 1 ) print (newarr) |
Output:
[1 2 3 4 5 6]