Python | Numpy matrix.squeeze ()

NumPy | Python Methods and Functions

With the matrix.squeeze() method we can compress the size of the matrix using the same method. But remember one thing: we are using this method on a matrix of Nx1 size, which is returned as a 1xN matrix.

Syntax: matrix.squeeze ()
Return: Return a squeezed matrix

Example # 1:
In this example, we can compress the size of the given matrix using matrix.squeeze () .

# import important module in python

import numpy as np

 
# make a matrix with NumPy

gfg = np.matrix ( `[[4], [12]]` )

  
# using the matrix.squeeze () method
gfg.squeeze ()

 

print (gfg)

Output:

 [[4 12]] 

Example # 2:

# import important module in python

import numpy as np

 
# make a matrix with NumPy

gfg = np.matrix ( `[4, 1, 9; 12, 3, 1; 4, 5, 6] ` )

  
# using the matrix.squeeze () method
gfg.squeeze ()

 

print (gfg)

Exit:

 [[4 1 9] [12 3 1] [4 5 6]] 




Get Solution for free from DataCamp guru