Python Numpy matrix.compress() Function

NumPy Library 

NumPy is a library in python that is created to work efficiently with arrays in python. It is fast, easy to learn, and provides efficient storage. It also provides a better way of handling data for the process. We can create an n-dimensional array in NumPy. To use NumPy simply have to import it into our program and then we can easily use the functionality of NumPy in our program.

NumPy is a Python library that is frequently used for scientific and statistical analysis. NumPy arrays are grids of the same datatype’s values.

Numpy matrix.compress() Function:

We can choose items from a matrix using the matrix.compress() method of the Numpy module by giving a parameter as an array with the value 0 to not include the element or 1 to include the element in a matrix. To Put simply, the boolean array is passed to the matrix.compress() method.

0 – NOT include/selected

1 – Include

Syntax:

 matrix.compress()

Return Value:

A compressed array is returned by the compress() function.

Numpy matrix.compress() Function in Python

For 1-Dimensional (1D) Matrix

Approach:

  • Import numpy module using the import keyword
  • Create a matrix(1-Dimensional) using the matrix() function of numpy module by passing some random 1D matrix as an argument to it and store it in a variable
  • Print the given matrix.
  • Pass the boolean array(same length as given matrix) i.e with 0/1, given matrix as arguments to the compress() function of the numpy module to select the specific elements from the given matrix to be included.
  • Here 0 means – NOT include, 1 means – Include
  • So, the values corresponding to 1 are only shown here.
  • Print the compressed matrix for the given matrix.
  • The Exit of the Program.

Below is the implementation:

# Import numpy module using the import keyword
import numpy as np
            
# Create a matrix(1-Dimensional) using the matrix() function of numpy module by passing 
# some random 1D matrix as an argument to it and store it in a variable
gvn_matrx = np.matrix('[2, 1, 6, 4, 7]')

# Print the given matrix
print("The given matrix is:") 
print(gvn_matrx)   

# Pass the boolean array(same length as given matrix) i.e with 0/1, given matrix 
# as arguments to the compress() function of the numpy module to select the
# specific elements from the given matrix to be included.
# Here 0 means - NOT include, 1 means - Include 
# So, the values corresponding to 1 are only shown here.
rslt = np.compress([1, 0, 0, 1, 1], gvn_matrx)

# Print the compressed matrix for the given matrix.     
print("The compressed matrix for the given matrix is:")
print(rslt)

Output:

The given matrix is:
[[2 1 6 4 7]]
The compressed matrix for the given matrix is:
[[2 4 7]]

For 3-Dimensional (3D) Matrix

Approach:

  • Import numpy module using the import keyword
  • Create a matrix(3-Dimensional) using the matrix() function of numpy module by passing some random 3D matrix as an argument to it and store it in a variable
  • Print the given matrix.
  • Pass the boolean array(same length as given matrix) i.e with 0/1, given matrix as arguments to the compress() function of the numpy module to select the specific elements from the given matrix to be included.
  • Here 0 means – NOT include, 1 means – Include
  • So, the values corresponding to 1 are only shown here.
  • Print the compressed matrix for the given matrix.
  • The Exit of the Program.

Below is the implementation:

# Import numpy module using the import keyword
import numpy as np
            
# Create a matrix(3-Dimensional) using the matrix() function of numpy module by passing 
# some random 3D matrix as an argument to it and store it in a variable
gvn_matrx = np.matrix('[10, 1, 4; 2, 7, 3; 8, 9, 5]')

# Print the given matrix
print("The given matrix is:") 
print(gvn_matrx)   

# Pass the boolean array(same length as given matrix) i.e with 0/1, given matrix 
# as arguments to the compress() function of the numpy module to select the
# specific elements from the given matrix to be included.
# Here 0 means - NOT include, 1 means - Include 
# So, the values corresponding to 1 are only shown here.
rslt = np.compress([1, 0, 0, 1, 1, 0, 1, 0, 1], gvn_matrx)

# Print the compressed matrix for the given matrix.     
print("The compressed matrix for the given matrix is:")
print(rslt)

Output:

The given matrix is:
[[10 1 4]
 [ 2 7 3]
 [ 8 9 5]]
The compressed matrix for the given matrix is:
[[10 2 7 8 5]]