mflowww - 1 year ago 139

Python Question

In Python, I have an ndarray

`y`

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

`y.count(0)`

`y.count(1)`

`'numpy.ndarray'`

What should I do?

Answer Source

http://docs.scipy.org/doc/numpy/reference/generated/numpy.ndarray.html

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.