Python | Numpy matrix.compress ()



Using the Numpy matrix.compress () method, we can select elements from the matrix by passing the parameter as an array that contains the value 0 to not include the element, or 1 to include the element in the matrix. Just pass the boolean array to matrix.compress () .

Syntax: matrix.compress ()

Return: Return a compressed array

Example # 1:
In this example, we can see that using the matrix.compress () method we can compress the matrix.

# import important module in python

import numpy as np

 
# make a matrix with NumPy

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

  
# using the matrix.compress () method

geeks = np.compress ([ 1 , 0 , 1 , 0 , 1 , 1 ], gfg)

  

print (geeks)

Exit:

 [[1 3 3 1]] 

Example # 2:

# import important module in python

import numpy as np < / code>

 
# make a matrix with NumPy

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

  
# using the matrix.compress () method

geeks = np.compress ([ 1 , 0 , 1 , 1 , 1 , 0 , 0 , 1 , 1 ], gfg )

 

print (geeks)

Exit:

 [[1 3 4 5 8 9] ]