by R4R Team

- 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)


output-

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]]




Leave a Comment:
Search
Categories
R4R Team
R4Rin Top Tutorials are Core Java,Hibernate ,Spring,Sturts.The content on R4R.in website is done by expert team not only with the help of books but along with the strong professional knowledge in all context like coding,designing, marketing,etc!