mflowww mflowww - 6 months ago 43
Python Question

How to count the occurrence of certain item in an ndarray in Python?

In Python, I have an ndarray


that is printed as
array([0, 0, 0, 1, 0, 1, 1, 0, 0, 0, 0, 1])

I'm trying to count how many 0 and how many 1 are there in this array.

But when I type
, it says
object has no attribute 'count'

What should I do?


check the link for what methods ndarray offers. There is no count function there. The only way is to write the count function yourself. It's easy, just iterate on each element and increment the counter when a given condition is met.