How to Identify the Non-zero Elements in a NumPy Array?

Learn via video courses
Topics Covered

Non-zero elements of a Numpy array, i.e., the elements which are not equal to zero in a Numpy array, can be identified in many ways. There are various methods, like np. non-zero function, np.argwhere function, and np.where function, by which we can find the non-zero elements and also their indexes. Here are the following techniques to identify the non-zero elements of a NumPy array.

Methods for Identifying the Non-Zero Elements in a Numpy Array

1. Using any() Function

Syntax:

This function checks if there is any element in the array that is non-zero. It returns true if the array contains non-zero elements and returns false if the array contains 0 value elements.

Let's see how any function works by following these examples:

Code 1:

Output:

Explanation: In the above example,anyfunction is used to check if an array arr has non-zero elements. As the array has non-zero elements like 1,3,7, etc., it returns true.

Code 2:

Output:

Explanation: An array arr of zero values of int datatype with shape 3 rows and 2 columns is created using np.zeros function. Checking non-zero elements using any function. As the array contains all zero values, False is returned as output.

In the following examples, by using a created variable or operation performed inside the arr enclosed by "[]" brackets as an index, non-zero elements are printed as output.

2. Using Non-zero Function

The Non-zero function is used to identify the indices of non-zero values in an array. It returns a tuple containing indices of the non-zero elements of the array. For each dimension, one tuple array.

Code 1:

Output:

Explanation: In the above example, indices of non-zero elements are found using the non-zero function and stored in the variable No_zero_ele. No_zero_ele gives us the tuple array of indices of non-zero elements.

Code 2:

Output:

Explanation: In the code, for example, a 1-D array is transformed into a 2-D array of 3 rows and 4 columns using the np.reshape function. Indices of non-zero elements in the No_zero_ele variable. Here we get two tuple arrays as output. The first output array contains row and column indicators of a 2-D array element; for example, 7 is a non-zero array element at the first row and third column.

3. Using Argwhere Function

This function is used to return the indices of non-zero elements of the array, grouped by elements. Its output seems like a 2-d matrix.

Code 1:

Output:

Explanation: Indices of non-zero elements of the above array are found using the argwhere function and stored in the No_zero_elevariable. Using theNo_zero_ele` variable inside the array enclosed by "[]," non-zero elements of the array are printed as output in a 2-D matrix, e.g., the output is of the shape (8,1), i.e., 8 rows and 1 column.

Code 2:

Output:

Explanation: By using the np.reshape function 1-D array is transformed into a 2-D array of shapes with 3 rows and 4 columns. By using the argwhere function, indices of non-zero elements of the array are returned as output, which looks like a 2-D matrix of 8 rows and 2 columns. The first element in the output array represents the row, and the second represents the column; for example, [0,0] represents the index of the element at the 0th row and 0th column, i.e., element 1.

4. Using Where Function

This function returns the indices of elements of the array as per the given condition.

Code 1:

Output:

Explanation: In the above code, inside the where function, there is a condition defined for the array element should not be equal to zero. The elements of the array which satisfied this condition had their indices in variable No_zero_ele and printed as output. When we use No_zero_ele inside the arr enclosed by "[]" braces, non-zero elements are printed.

Code 2:

Output:

Explanation: In the above example, using the np.reshape function, a 1-D array is reshaped into a 2-D array of 3 rows and 4 columns. There is a condition defined inside the where function; that is, an array element should not be equal to zero. Indices of non-zero elements of an array are printed as output. Here are two output arrays as output for indices; one is for rows, and the other is for columns.

Conclusion

  • NumPy elements with zero values are termed non-zero elements of the NumPy array.
  • We can check whether the array contains non-zero elements or not by using the any function.
  • The non-zero, argwhere, and where functions can be used to identify non-zero elements and their indices.