Python Numpy matrix.nonzero() 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.nonzero() Function:

we can obtain the index value of a nonzero value from a specified matrix using the matrix.nonzero() function of the NumPy module. It always returns a two-dimensional array.

Syntax:

 matrix.nonzero()

Return Value:

The index value of the nonzero value from the given matrix is returned by the nonzero() function.

Numpy matrix.nonzero() Function in Python

For 2-Dimensional (2D) Matrix

Approach:

  • Import numpy module using the import keyword
  • Create a matrix(2-Dimensional) using the matrix() function of numpy module by passing some random 2D matrix as an argument to it and store it in a variable
  • Apply nonzero() function on the given matrix to get the index values of nonzero elements from the given matrix.
  • Store it in another variable
  • Print the index values of nonzero elements from 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(2-Dimensional) using the matrix() function of numpy module by passing 
# some random 2D matrix as an argument to it and store it in a variable
gvn_matrx = np.matrix('[2, 0; 6, 3]')
            
# Apply nonzero() function on the given matrix to get the index values of nonzero elements from the given matrix
# Store it in another variable
rslt = gvn_matrx.nonzero()
# Print the index values of nonzero elements from the given matrix
print("The index values of nonzero elements from the given matrix:")
print(rslt)

Output:

The index values of nonzero elements from the given matrix:
(array([0, 1, 1]), array([0, 0, 1]))

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
  • Apply nonzero() function on the given matrix to get the index values of nonzero elements from the given matrix.
  • Store it in another variable
  • Print the index values of nonzero elements from 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('[0, 4, 1; 8, 0, 3; 0, 9, 5]')
            
# Apply nonzero() function on the given matrix to get the index values of nonzero elements from the given matrix
# Store it in another variable
rslt = gvn_matrx.nonzero()
# Print the index values of nonzero elements from the given matrix
print("The index values of nonzero elements from the given matrix:")
print(rslt)

Output:

The index values of nonzero elements from the given matrix:
(array([0, 0, 1, 1, 2, 2]), array([1, 2, 0, 2, 1, 2]))