How can the number of elements equal to zero be counted using NumPy?

How can the number of elements equal to zero be counted using NumPy?

NumPy is a powerful Python library used for scientific computing and data analysis. One of its useful functions is the ability to count the number of elements equal to zero in an array. This can be achieved by using the “np.count_nonzero()” function, which returns the total count of non-zero elements in an array. By subtracting this value from the total number of elements in the array, the number of elements equal to zero can be obtained. This feature of NumPy provides a convenient and efficient way to analyze and manipulate data containing zero values.

NumPy: Count Number of Elements Equal to Zero


You can use the following basic syntax to count the number of elements equal to zero in a NumPy array:

import numpy as np

np.count_nonzero(my_array==0)

This particular example will return the number of elements equal to zero in the NumPy array called my_array.

The following example shows how to use this syntax in practice.

Example: Count Number of Elements Equal to Zero in NumPy Array

The following code shows how to use the count_nonzero() function to count the number of elements in a NumPy array equal to zero:

import numpy as np

#create NumPy array
my_array = np.array([2, 0, 0, 4, 5, 9, 12, 0, 4, 13, 15, 19])

#count number of values in array equal to zero
np.count_nonzero(my_array==0)

3

From the output we can see that 3 values in the NumPy array are equal to zero.

We can manually look at the NumPy array to verify that there are indeed three elements equal to zero in the array.

If you would instead like to count the number of elements not equal to zero, you can use the count_nonzero() function as follows:

import numpy as np

#create NumPy array
my_array = np.array([2, 0, 0, 4, 5, 9, 12, 0, 4, 13, 15, 19])

#count number of values in array not equal to zero
np.count_nonzero(my_array)

9

From the output we can see that 9 values in the NumPy array are not equal to zero.

Note: If you have any NaN values in your NumPy array, the count_nonzero() function will count each NaN value as an element not equal to zero.

The following tutorials explain how to perform other common operations in Python:

Cite this article

stats writer (2024). How can the number of elements equal to zero be counted using NumPy?. PSYCHOLOGICAL SCALES. Retrieved from https://scales.arabpsychology.com/stats/how-can-the-number-of-elements-equal-to-zero-be-counted-using-numpy/

stats writer. "How can the number of elements equal to zero be counted using NumPy?." PSYCHOLOGICAL SCALES, 24 Jun. 2024, https://scales.arabpsychology.com/stats/how-can-the-number-of-elements-equal-to-zero-be-counted-using-numpy/.

stats writer. "How can the number of elements equal to zero be counted using NumPy?." PSYCHOLOGICAL SCALES, 2024. https://scales.arabpsychology.com/stats/how-can-the-number-of-elements-equal-to-zero-be-counted-using-numpy/.

stats writer (2024) 'How can the number of elements equal to zero be counted using NumPy?', PSYCHOLOGICAL SCALES. Available at: https://scales.arabpsychology.com/stats/how-can-the-number-of-elements-equal-to-zero-be-counted-using-numpy/.

[1] stats writer, "How can the number of elements equal to zero be counted using NumPy?," PSYCHOLOGICAL SCALES, vol. X, no. Y, ص Z-Z, June, 2024.

stats writer. How can the number of elements equal to zero be counted using NumPy?. PSYCHOLOGICAL SCALES. 2024;vol(issue):pages.

Download Post (.PDF)
Slide Up
x
PDF
Scroll to Top