Reshape NumPy Array
Last Updated :
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 formed
Return : It returns numpy.ndarray
Note : We can also use np.reshape(array, shape) command to reshape the array
Reshaping : 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
import numpy as np
array = np.array([ 1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 , 9 , 10 , 11 , 12 , 13 , 14 , 15 , 16 ])
print ( "Array : " + str (array))
n = array.size
N = 4
M = n / / N
reshaped1 = array.reshape((N, M))
print ( "First Reshaped Array : " )
print (reshaped1)
reshaped2 = np.reshape(array, ( 2 , 8 ))
print ( "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
import numpy as np
array = np.array([ 1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 , 9 , 10 , 11 , 12 , 13 , 14 , 15 , 16 ])
print ( "Array : " + str (array))
reshaped = array.reshape(( 2 , 2 , 4 ))
print ( "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
import numpy as np
array = np.array([[ 1 , 2 , 3 ],
[ 4 , 5 , 6 ],
[ 7 , 8 , 9 ]])
print ( " 2-D Array : " )
print (array)
reshaped = array.reshape(( 9 ))
print ( "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
import numpy as np
array = np.array([ 1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 , 9 , 10 , 11 , 12 , 13 , 14 , 15 , 16 ])
print ( "Array : " + str (array))
reshaped1 = array.reshape(( 2 , 2 , - 1 ))
print ( "First Reshaped Array : " )
print (reshaped1)
reshaped2 = array.reshape(( 4 , - 1 ))
print ( "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
import numpy as np
array = np.array([[ 1 , 2 , 3 ],
[ 4 , 5 , 6 ],
[ 7 , 8 , 9 ]])
print ( " 2-D Array : " )
print (array)
reshaped = array.reshape(( 1 , 5 ))
print ( "Reshaped 1-D Array : " )
print (reshaped)
|
ValueError: cannot reshape array of size 9 into shape (1, 5)
Please Login to comment...