How to check whether the element of a given NumPy array is non-zero?


There are multiple ways to check whether the element of a given Numpy array is Non-zero. Here are few common ways that we can apply.

Using Boolean indexing

Boolean Indexing is a technique in Numpy library, that allows for the selection of specific elements from the array based on the Boolean condition. This creates a Boolean mask containing True or False values, which have the same shape and size as per the Boolean condition.

Example

Following example how to use Boolean indexing to check whether the element of a given numpy array is non-zero.

import numpy as np
arr = np.arange(2,20,3)
if np.all(arr) >0:
   print("The given array is Non-zero")
else:
   print("The given array is zero")

Output

When we run the above code, following output will be generated, here, the output determines that the given array is non-zero.

The given array is Non-zero

Example

Let’s see another example in which Boolean indexing is applied on the 2-d array.

import numpy as np
arr = np.arange(2,20,3).reshape(3,2)
print("The original array:",arr)
if np.all(arr) > 0:
    print("The given array is Non-zero")
else:
    print("The given array is zero")

Output

Following is the output of the Boolean indexing, when we run the above code -

The original array: [[ 2  5]
 [ 8 11]
 [14 17]]
The given array is Non-zero

Using nonzero() function

In python, the nonzero() function is used to retrieve indices of the non-zero elements in the array.

Example

The following is the example of the non_zero() function.

import numpy as np
arr = np.arange(2,20,3).reshape(3,2)
print("The original array:",arr)
if np.nonzero(arr):
    print("The given array is Non-zero")
else:
    print("The given array is zero")

Output

When we run the above code, following output will be generated -

The original array: [[ 2  5]
 [ 8 11]
 [14 17]]
The given array is Non-zero

Example

Let’s see another example to work with the non_zero() function of the numpy library.

import numpy as np
arr = np.arange(0,20,2)
print("The original array:",arr)
non_zero = np.nonzero(arr)
print(non_zero)

Output

The below is the output of the non_zero() function.

The original array: [ 0  2  4  6  8 10 12 14 16 18]
(array([1, 2, 3, 4, 5, 6, 7, 8, 9], dtype=int64),)

Using np.where() function

Where() is another function provided by the numpy library, this function is used to check if elements in the given array are non-zero. When called with a specified array, Where() returns the indices of the non-zero elements in the array.

Example

In the following example, we will find the indices of the non-zero elements in a NumPy array using Where() function, we are passing an array and the value ‘0’ as arguments to the function, in order to retrieve indices.

import numpy as np
arr = np.array([[[10,30],[2,40.3]],[[56,4],[56,3]]])
print("The Original array:",arr)
output = np.where(arr == 0)
print(output)

Output

When we run the above code, following output will be generated -

The Original array: [[[10.  30. ]
  [ 2.  40.3]]

 [[56.   4. ]
  [56.   3. ]]]
  (array([], dtype=int64), array([], dtype=int64), array([], dtype=int64))

Example

Let’s see another example to check whether the non-zero elements are present in the given array, using the Where() function.

import numpy as np
arr = np.array([10,302,4,0.356,4,3,0])
print("The Original array:",arr)
output = np.where(arr == 0)
print(output)

Output

When we run the above code, following output will be displayed. An array with the index as there is zero element is displayed as the output.

The Original array: [ 10.    302.      4.      0.356   4.      3.      0.   ]
(array([6], dtype=int64),)

Using numpy.count_nonzero() function

Another way to determine the non zero elements in the defined numpy array is using the count_nonzero() function. This function returns the count of the non_zero elements present in the array as the output.

Example

The following is the example.

import numpy as np
arr = np.array([10,302,4,0.356,4,3,0])
print("The Original array:",arr)
output = np.count_nonzero(arr)
print("There is/are",output,"zeroes in the defined array")

Output

When we run the above code, following output will be generated -

The Original array: [  1  32   4 356   4   3   0]
There is/are 6 zeroes in the defined array

Updated on: 09-Aug-2023

95 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements