I have a NumPy array ‘boolarr’ of boolean type. I want to count the number of elements whose values are True. Is there a NumPy or Python routine dedicated for this task? Or, do I need to iterate over the elements in my script?
I have a NumPy array ‘boolarr’ of boolean type. I want to count the
Share
You have multiple options. Two options are the following.
Here’s an example:
Of course, that is a
bool-specific answer. More generally, you can usenumpy.count_nonzero.