Skip to content
Related Articles

Related Articles

Python | Numpy matrix.reshape()
  • Last Updated : 18 Apr, 2019

With the help of Numpy matrix.reshape() method, we are able to reshape the shape of the given matrix. Remember all elements should be covered after reshaping the given matrix.

Syntax : matrix.reshape(shape)

Return: new reshapped matrix

Example #1 :
In the given example we are able to reshape the given matrix by using matrix.reshape() method.




# import the important module in python
import numpy as np
           
# make matrix with numpy
gfg = np.matrix('[64, 1; 12, 3]')
           
# applying matrix.reshape() method
geeks = gfg.reshape((1, 4))
     
print(geeks)


Output:



[[64  1 12  3]]

Example #2 :




# import the important module in python
import numpy as np
           
# make a matrix with numpy
gfg = np.matrix('[1, 2; 4, 5; 7, 8]')
           
# applying matrix.reshape() method
geeks = gfg.reshape((2, 3))
     
print(geeks)


Output:

[[1 2 4]
 [5 7 8]]

Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.

To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course.

My Personal Notes arrow_drop_up
Recommended Articles
Page :