NumPy nonzero() Function:
NP.nonzero: The nonzero() function of the NumPy module returns the indices of non-zero elements. The indices of the non-zero elements in each dimension of “a” are returned as a tuple of arrays, one for each dimension of “a.” The values in “a” is always checked and returned in C-style row-major order.
Syntax:
numpy.nonzero(a)
Parameters
a: This is required. It is the array given as input
Return Value:
The indices of elements that are non-zero are returned
NumPy nonzero() Function in Python
Example1
Approach:
- Import numpy module using the import keyword.
- Pass some random list as an argument to the array() function to create an array.
- Store it in a variable.
- Print the above-given array.
- Pass the given array as an argument to the nonzero() function of numpy module to get all the indices of the non-zero elements in the given array.
- Store it in another variable.
- Print the indices of all non-zero elements in the given array.
- Print all the non-zero elements in the given array.
- The Exit of the Program.
Below is the implementation:
# Import numpy module using the import keyword import numpy as np # Pass some random list as an argument to the array() function to # create an array. # Store it in a variable. gvn_arry = np.array([5, 0, 0, 35, 40, 0]) # Print the above given array. print("The above given array is:") print(gvn_arry) # Pass the given array as an argument to the nonzero() function of numpy module # to get all the indices of the non-zero elements in the given array. # Store it in another variable. rslt = np.nonzero(gvn_arry) # Print the indices of all non-zero elements in the given array. print("The indices of all non-zero elements in the given array:") print(rslt) # Print all the non-zero elements in the given array. print("The non-zero elements in the given array:") print(gvn_arry[rslt])
Output:
The above given array is: [ 5 0 0 35 40 0] The indices of all non-zero elements in the given array: (array([0, 3, 4]),) The non-zero elements in the given array: [ 5 35 40]
Example2: For 2D Arrays
Approach:
- Import numpy module using the import keyword.
- Pass some random list(2-Dimensional) as an argument to the array() function to create an array.
- Store it in a variable.
- Print the above-given 2D array.
- Pass the given array as an argument to the nonzero() function of numpy module to get all the indices of the non-zero elements in the given 2D array.
- Store it in another variable.
- Print the indices of all non-zero elements in the given 2D array.
- Print all the non-zero elements in the given 2D array.
- The Exit of the Program.
Below is the implementation:
# Import numpy module using the import keyword import numpy as np # Pass some random list(2-Dimensional) as an argument to the array() function to # create an array. # Store it in a variable. gvn_arry = np.array([[3, 0, 1], [2, 0, 0], [8, 4, 0]]) # Print the above given array. print("The above given array 2D is:") print(gvn_arry) # Pass the given array as an argument to the nonzero() function of numpy module # to get all the indices of the non-zero elements in the given array. # Store it in another variable. rslt = np.nonzero(gvn_arry) # Print the indices of all non-zero elements in the given 2D array. print("The indices of all non-zero elements in the given 2D array:") print(rslt) # Print all the non-zero elements in the given 2D array. print("The non-zero elements in the given 2D array:") print(gvn_arry[rslt])
Output:
The above given array 2D is: [[3 0 1] [2 0 0] [8 4 0]] The indices of all non-zero elements in the given 2D array: (array([0, 0, 1, 2, 2]), array([0, 2, 0, 0, 1])) The non-zero elements in the given 2D array: [3 1 2 8 4]
Example3
The nonzero() method can be used with a condition.
Below is the implementation:
# Import numpy module using the import keyword import numpy as np # Pass some random list as an argument to the array() function to # create an array. # Store it in a variable. gvn_arry = np.array([40, 62, 15, 76, 80, 5, 0, 18]) # Print the above given array. print("The above given array is:") print(gvn_arry) # Pass the given array as an argument to the nonzero() function of numpy module # to get all the elements in the array that satisfies the given condition. # Store it in another variable. rslt = np.nonzero(gvn_arry < 50) # Print the indices of the elements in the given array that satisfies the given condition. print("The indices of the elements in the given array that satisfies the given condition:") print(rslt) # Print the elements in the given array that satisfies the given condition. print("The elements in the given array that satisfies the given condition:") print(gvn_arry[rslt])
Output:
The above given array is: [40 62 15 76 80 5 0 18] The indices of the elements in the given array that satisfies the given condition: (array([0, 2, 5, 6, 7]),) The elements in the given array that satisfies the given condition: [40 15 5 0 18]