Reshape NumPy Array

20 Jan, 2022
NumPy is a general-purpose array-processing package. It provides a high-performance multidimensional array object, and tools for working with these arrays. It is the fundamental package for scientific computing with Python. Numpy is basically used for creating array of n dimensions.Reshaping numpy array simply means changing the shape of the given array, shape basically tells the number of elements and dimension of array, by reshaping an array we can add or remove dimensions or change number of elements in each dimension.In order to reshape a numpy array we use reshape method with the given array.  
Syntax : array.reshape(shape)Argument : It take tuple as argument, tuple is the new shape to be formedReturn : It returns numpy.ndarray 
Note : We can also use np.reshape(array, shape) command to reshape the arrayReshaping : 1-D to 2D In this example we will reshape the 1-D array of shape (1, n) to 2-D array of shape (N, M) here M should be equal to the n/N there for N should be factor of n.  
Python3
# importing numpyimport numpy as np # creating a numpy arrayarray = np.array([1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16]) # printing arrayprint("Array : " + str(array)) # length of arrayn = array.size # N-D array N dimensionN = 4 # calculating MM = n//N # reshaping numpy array# converting it to 2-D from 1-D arrayreshaped1 = array.reshape((N, M)) # printing reshaped arrayprint("First Reshaped Array : ")print(reshaped1) # creating another reshaped arrayreshaped2 = np.reshape(array, (2, 8)) # printing reshaped arrayprint("Second Reshaped Array : ")print(reshaped2)
Output :  
Array : [ 1  2  3  4  5  6  7  8  9 10 11 12 13 14 15 16]
First Reshaped Array : 
[[ 1  2  3  4]
 [ 5  6  7  8]
 [ 9 10 11 12]
 [13 14 15 16]]
Second Reshaped Array : 
[[ 1  2  3  4  5  6  7  8]
 [ 9 10 11 12 13 14 15 16]]
Reshaping : 1-D to 3-D In this we will see how we can reshape a 1-D array to 3-D dimension array. A 3-D array is the 1-D array of 2-D arrays.  
Python3
# importing numpyimport numpy as np # creating a numpy arrayarray = np.array([1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16]) # printing arrayprint("Array : " + str(array))  # reshaping numpy array# converting it to 3-D from 1-D arrayreshaped = array.reshape((2, 2, 4)) # printing reshaped arrayprint("Reshaped 3-D Array : ")print(reshaped)
Output :  
Array : [ 1  2  3  4  5  6  7  8  9 10 11 12 13 14 15 16]
Reshaped 3-D Array : 
[[[ 1  2  3  4]
  [ 5  6  7  8]]

 [[ 9 10 11 12]
  [13 14 15 16]]]
Reshaping N-D to 1-D array In this example we will see how we can reshape a 2-D or 3-D array to form a 1-D array. We can also use reshape(-1) to do this, here -1 is the unknown dimension.  
Python3
# importing numpyimport numpy as np # creating a numpy arrayarray = np.array([[1, 2, 3],                 [4, 5, 6],                 [7, 8, 9]]) # printing arrayprint(" 2-D Array : ")print(array)  # reshaping numpy array# converting it to 1-D from 2-D arrayreshaped = array.reshape((9)) # or we can use unknown dimension# reshaped = array.reshape((-1)) # printing reshaped arrayprint("Reshaped 1-D Array : ")print(reshaped)
Output :  
 2-D Array : 
[[1 2 3]
 [4 5 6]
 [7 8 9]]
Reshaped 1-D Array : 
[[1 2 3 4 5 6 7 8 9]]
Reshaping using unknown dimension We can reshape a array although we don’t know all the new dimensions by using -1 as one of the dimension, but we should know all the other dimension to use unknown dimension  
Python3
# importing numpyimport numpy as np # creating a numpy arrayarray = np.array([1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16]) # printing arrayprint("Array : " + str(array))  # reshaping numpy array# converting it to 3-D from 1-D arrayreshaped1 = array.reshape((2, 2, -1)) # printing reshaped arrayprint("First Reshaped Array : ")print(reshaped1)  # converting it to 2-D arrayreshaped2 = array.reshape((4, -1)) # printing reshaped arrayprint("Second Reshaped Array : ")print(reshaped2)
Output :  
Array : [ 1  2  3  4  5  6  7  8  9 10 11 12 13 14 15 16]
First Reshaped Array : 
[[[ 1  2  3  4]
  [ 5  6  7  8]]

 [[ 9 10 11 12]
  [13 14 15 16]]]
Second Reshaped Array : 
[[ 1  2  3  4]
 [ 5  6  7  8]
 [ 9 10 11 12]
 [13 14 15 16]]
Errors Occur during reshaping When we try to reshape a array to a shape which is not mathematically possible then value error is generated saying can not reshape the array. For example when we try to reshape 1-D array with 4 elements into a 2-D array of dimension(3, 3) is not possible as new array requires 9 elements  
Python3
# importing numpyimport numpy as np # creating a numpy arrayarray = np.array([[1, 2, 3],                 [4, 5, 6],                 [7, 8, 9]]) # printing arrayprint(" 2-D Array : ")print(array)  # reshaping numpy array# converting it to 1-D from 2-D array# reshaping it into 1, 5reshaped = array.reshape((1, 5)) # or we can use # printing reshaped arrayprint("Reshaped 1-D Array : ")print(reshaped)
ValueError: cannot reshape array of size 9 into shape (1, 5)
adnanirshad158
sweetyty
rkbhola5
Python numpy-arrayManipulation
Python-numpy
Python
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
How to Install PIP on Windows ?
Python Classes and Objects
Python OOPs Concepts
Introduction To PYTHON
Python | os.path.join() method
How to drop one or multiple columns in Pandas Dataframe
How To Convert Python Dictionary To JSON?
Check if element exists in list in Python
Python | Get unique values from a list
Python | datetime.timedelta() function