- reshape() is Python Numpy method which change the shape of the given array or matrix.
- It required array and new shape as argument to call this method.
Syntax-
numpy.reshape(array,shape)
-Here shape is in tuple i.e. (row,column)
Example-
array is [1 2 3 4]
then reshape(array,(2,2)) will return
[[1 2]
[3 4]]
program-
#import numpy
import numpy as np
l=[1,2,3,4,5,6,7,8]
print("Given matrix is:",np.array(l))
#convert matrix in order 4x2
arr=np.reshape(l,(4,2))
print("Now matrix is:n",arr)
#convert matrix in order 2x4
arr=np.reshape(l,(2,4))
print("Now matrix is:n",arr)
#convert matrix in order 8x1
arr=np.reshape(l,(8,1))
print("Now matrix is:n",arr)
Given matrix is: [1 2 3 4 5 6 7 8]
Now matrix is:
[[1 2]
[3 4]
[5 6]
[7 8]]
Now matrix is:
[[1 2 3 4]
[5 6 7 8]]
Now matrix is:
[[1]
[2]
[3]
[4]
[5]
[6]
[7]
[8]]