How to count the number of True elements in a NumPy boolean array in Python

Counting the number of True elements in a NumPy array of booleans returns the total number of elements equal to True in the array. For example, counting the number of True elements in [True, False, True, True, False] returns 3.

Solution for How to count the number of True elements in a NumPy boolean array in Python : You can use numpy.count_nonzero() to count the number of True elements in a boolean array Call numpy.count_nonzero(array) to return the number of True elements in a boolean array.


how-to-count-the-number-of-true-elements-in-a-numpy-boolean-array-in-python