Communities for your favorite technologies. Explore all Collectives
Stack Overflow for Teams is now called Stack Internal. Bring the best of human thought and AI automation together at your work.
Bring the best of human thought and AI automation together at your work. Learn more
Find centralized, trusted content and collaborate around the technologies you use most.
Stack Internal
Knowledge at work
Bring the best of human thought and AI automation together at your work.
How do I count the number of 0s and 1s in the following array?
0
1
y = np.array([0, 0, 0, 1, 0, 1, 1, 0, 0, 0, 0, 1])
y.count(0) gives:
y.count(0)
numpy.ndarray object has no attribute count
numpy.ndarray
count
numpy.count_nonzero
Numpy has a module for this. Just a small hack. Put your input array as bins.
numpy.histogram(y, bins=y)
The output are 2 arrays. One with the values itself, other with the corresponding frequencies.
Add a comment
using numpy.count $ a = [0, 0, 0, 1, 0, 1, 1, 0, 0, 0, 0, 1] $ np.count(a, 1)
Start asking to get answers
Find the answer to your question by asking.
Explore related questions
See similar questions with these tags.
numpy.count_nonzero.