How can I count the number of elements in a NumPy array that are equal to True?

How can I count the number of elements in a NumPy array that are equal to True?

To count the number of elements in a NumPy array that are equal to True, one can use the np.count_nonzero() function. This function takes in the array as an argument and returns the number of non-zero elements, which in this case, would be the elements that are equal to True. This method is efficient and straightforward, making it a reliable way to determine the number of elements in a NumPy array that fulfill a specific condition.

NumPy: Count Number of Elements Equal to True


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

import numpy as np

np.count_nonzero(my_array)

This particular example will return the number of elements equal to True 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 True 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 True:

import numpy as np

#create NumPy array
my_array = np.array([True, False, False, False, True, True, False, True, True])

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

5

From the output we can see that 5 values in the NumPy array are equal to True.

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

If you would instead like to count the number of element equal to False, you can subtract the results from the count_nonzero() function from the size() function as follows:

import numpy as np

#create NumPy array
my_array = np.array([True, False, False, False, True, True, False, True, True])

#count number of values in array equal to False
np.size(my_array) - np.count_nonzero(my_array)

4

From the output we can see that 4 values in the NumPy array are equal to False.

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

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

Cite this article

stats writer (2024). How can I count the number of elements in a NumPy array that are equal to True?. PSYCHOLOGICAL SCALES. Retrieved from https://scales.arabpsychology.com/stats/how-can-i-count-the-number-of-elements-in-a-numpy-array-that-are-equal-to-true/

stats writer. "How can I count the number of elements in a NumPy array that are equal to True?." PSYCHOLOGICAL SCALES, 24 Jun. 2024, https://scales.arabpsychology.com/stats/how-can-i-count-the-number-of-elements-in-a-numpy-array-that-are-equal-to-true/.

stats writer. "How can I count the number of elements in a NumPy array that are equal to True?." PSYCHOLOGICAL SCALES, 2024. https://scales.arabpsychology.com/stats/how-can-i-count-the-number-of-elements-in-a-numpy-array-that-are-equal-to-true/.

stats writer (2024) 'How can I count the number of elements in a NumPy array that are equal to True?', PSYCHOLOGICAL SCALES. Available at: https://scales.arabpsychology.com/stats/how-can-i-count-the-number-of-elements-in-a-numpy-array-that-are-equal-to-true/.

[1] stats writer, "How can I count the number of elements in a NumPy array that are equal to True?," PSYCHOLOGICAL SCALES, vol. X, no. Y, ص Z-Z, June, 2024.

stats writer. How can I count the number of elements in a NumPy array that are equal to True?. PSYCHOLOGICAL SCALES. 2024;vol(issue):pages.

Download Post (.PDF)
PDF
Scroll to Top