mflowww mflowww - 3 years ago 275
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?

Answer Source

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.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download