How to count the appearance of a specific element in ndarray in Python?

In Python, I have ndarray y which prints as array([0, 0, 0, 1, 0, 1, 1, 0, 0, 0, 0, 1])

I am trying to calculate how many 0 and how many 1 in this array.

But when I am y.count(0) or y.count(1) , it says:

The numpy.ndarray object numpy.ndarray not have a count attribute

What should I do?

+283
python numpy multidimensional-array count
Feb 22 '15 at 22:05
source share
27 answers
 >>> a = numpy.array([0, 3, 0, 1, 0, 1, 2, 1, 0, 0, 0, 0, 1, 3, 4]) >>> unique, counts = numpy.unique(a, return_counts=True) >>> dict(zip(unique, counts)) {0: 7, 1: 4, 2: 1, 3: 2, 4: 1} 

Non-numpy way :

Use collections.Counter

 >> import collections, numpy >>> a = numpy.array([0, 3, 0, 1, 0, 1, 2, 1, 0, 0, 0, 0, 1, 3, 4]) >>> collections.Counter(a) Counter({0: 7, 1: 4, 3: 2, 2: 1, 4: 1}) 
+460
Feb 22 '15 at
source share

How about using numpy.count_nonzero , something like

 >>> import numpy as np >>> y = np.array([1, 2, 2, 2, 2, 0, 2, 3, 3, 3, 0, 0, 2, 2, 0]) >>> np.count_nonzero(y == 1) 1 >>> np.count_nonzero(y == 2) 7 >>> np.count_nonzero(y == 3) 3 
+203
Feb 22 '16 at 9:14
source share

Personally, I would go: (y == 0).sum() and (y == 1).sum()

eg.

 import numpy as np y = np.array([0, 0, 0, 1, 0, 1, 1, 0, 0, 0, 0, 1]) num_zeros = (y == 0).sum() num_ones = (y == 1).sum() 
+109
May 05 '16 at 20:51
source share

In your case, you can also look at numpy.bincount

 In [56]: a = np.array([0, 0, 0, 1, 0, 1, 1, 0, 0, 0, 0, 1]) In [57]: np.bincount(a) Out[57]: array([8, 4]) #count of zeros is at index 0 : 8 #count of ones is at index 1 : 4 
+32
Feb 22 '15 at 23:45
source share

Convert array y to list l , then do l.count(1) and l.count(0)

 >>> y = numpy.array([0, 0, 0, 1, 0, 1, 1, 0, 0, 0, 0, 1]) >>> l = list(y) >>> l.count(1) 4 >>> l.count(0) 8 
+19
Feb 22 '15 at 10:12
source share
 y = np.array([0, 0, 0, 1, 0, 1, 1, 0, 0, 0, 0, 1]) 

If you know that they are just 0 and 1 :

 np.sum(y) 

gives you the number of units. np.sum(1-y) gives zeros.

For a little generality, if you want to count 0 , not zero (but maybe 2 or 3):

 np.count_nonzero(y) 

gives a number other than zero.

But if you need something more complex, I don't think numpy will provide a nice count option. In this case, go to the collection:

 import collections collections.Counter(y) > Counter({0: 8, 1: 4}) 

It behaves like a dict

 collections.Counter(y)[0] > 8 
+15
Feb 22 '15 at
source share

If you know exactly which number you are looking for, you can use the following:

 lst = np.array([1,1,2,3,3,6,6,6,3,2,1]) (lst == 2).sum() 

returns the number of times 2 in your array.

+13
Nov 22 '17 at 10:31 on
source share

What about len(y[y==0]) and len(y[y==1]) ?

+6
Mar 11 '16 at 18:29
source share

y.tolist().count(val)

with val 0 or 1

Since the python list has its own count function, converting to a list before using this function is a simple solution.

+6
19 '16 at 19:14
source share

Honestly, it’s easiest for me to switch to the pandas or DataFrame series:

 import pandas as pd import numpy as np df = pd.DataFrame({'data':np.array([0, 0, 0, 1, 0, 1, 1, 0, 0, 0, 0, 1])}) print df['data'].value_counts() 

Or this nice one-line movie suggested by Robert Muil:

 pd.Series([0, 0, 0, 1, 0, 1, 1, 0, 0, 0, 0, 1]).value_counts() 
+6
Oct 28 '16 at 17:58
source share

Another simple solution might be to use numpy.count_nonzero () :

 import numpy as np y = np.array([0, 0, 0, 1, 0, 1, 1, 0, 0, 0, 0, 1]) y_nonzero_num = np.count_nonzero(y==1) y_zero_num = np.count_nonzero(y==0) y_nonzero_num 4 y_zero_num 8 

Do not let the name fool you if you use it with a logical one, as in the example, it will do the trick.

+5
04 Oct '16 at 9:30
source share

To count the number of occurrences, you can use np.unique(array, return_counts=True) :

 In [75]: boo = np.array([0, 0, 0, 1, 0, 1, 1, 0, 0, 0, 0, 1]) # use bool value 'True' or equivalently '1' In [77]: uniq, cnts = np.unique(boo, return_counts=1) In [81]: uniq Out[81]: array([0, 1]) #unique elements in input array are: 0, 1 In [82]: cnts Out[82]: array([8, 4]) # 0 occurs 8 times, 1 occurs 4 times 
+5
Dec 23 '16 at 19:57
source share

No one suggested using numpy.bincount(input, minlength) with minlength = np.size(input) , but it seems like a good solution and, of course, the fastest:

 In [1]: choices = np.random.randint(0, 100, 10000) In [2]: %timeit [ np.sum(choices == k) for k in range(min(choices), max(choices)+1) ] 100 loops, best of 3: 2.67 ms per loop In [3]: %timeit np.unique(choices, return_counts=True) 1000 loops, best of 3: 388 µs per loop In [4]: %timeit np.bincount(choices, minlength=np.size(choices)) 100000 loops, best of 3: 16.3 µs per loop 

This is a crazy acceleration between numpy.unique(x, return_counts=True) and numpy.bincount(x, minlength=np.max(x)) !

+5
Mar 17 '17 at 16:19
source share

I would use np.where:

 how_many_0 = len(np.where(a==0.)[0]) how_many_1 = len(np.where(a==1.)[0]) 
+4
Oct 19 '15 at 14:15
source share

You can use dictionary understanding to create a neat single-line layer. More about understanding the word can be found here.

 >>>counts = {int(value): list(y).count(value) for value in set(y)} >>>print(counts) {0: 8, 1: 4} 

This will create a dictionary with values ​​in your ndarray as keys and value values ​​as values ​​for keys respectively.

This will work when you want to count the number of values ​​in arrays of this format.

+2
Dec 03 '18 at 16:12
source share

This can be done easily in the following way.

 y = np.array([0, 0, 0, 1, 0, 1, 1, 0, 0, 0, 0, 1]) y.tolist().count(1) 
+1
Oct 21 '16 at 21:37
source share

General and simple answer:

 numpy.sum(MyArray==x) # sum of a binary list of the occurence of x (=0 or 1) in MyArray 

which would lead to this complete code as an example

 import numpy MyArray=numpy.array([0, 0, 0, 1, 0, 1, 1, 0, 0, 0, 0, 1]) # array we want to search in x=0 # the value I want to count (can be iterator, in a list, etc.) numpy.sum(MyArray==0) # sum of a binary list of the occurence of x in MyArray 

Now, if MyArray is in several dimensions , and you want to calculate the presence of the distribution of values ​​in the row (= template in the future)

 MyArray=numpy.array([[6, 1],[4, 5],[0, 7],[5, 1],[2, 5],[1, 2],[3, 2],[0, 2],[2, 5],[5, 1],[3, 0]]) x=numpy.array([5,1]) # the value I want to count (can be iterator, in a list, etc.) temp = numpy.ascontiguousarray(MyArray).view(numpy.dtype((numpy.void, MyArray.dtype.itemsize * MyArray.shape[1]))) # convert the 2d-array into an array of analyzable patterns xt=numpy.ascontiguousarray(x).view(numpy.dtype((numpy.void, x.dtype.itemsize * x.shape[0]))) # convert what you search into one analyzable pattern numpy.sum(temp==xt) # count of the searched pattern in the list of patterns 
+1
Nov 18 '16 at 16:04
source share

Since your ndarray only contains 0 and 1, you can use sum () to get the appearance of 1s and len () - sum () to get the occurrence of 0s.

 num_of_ones = sum(array) num_of_zeros = len(array)-sum(array) 
+1
Jan 12 '17 at 17:40
source share

You have a special array with only 1 and 0 here. So the trick is to use

 np.mean(x) 

which gives you a percentage of 1 s in your array. Alternatively use

 np.sum(x) np.sum(1-x) 

will give you the absolute number 1 and 0 in your array.

+1
May 6 '19 at 16:28
source share

use the methods offered by the series:

 >>> import pandas as pd >>> y = np.array([0, 0, 0, 1, 0, 1, 1, 0, 0, 0, 0, 1]) >>> pd.Series(y).value_counts() 0 8 1 4 dtype: int64 
+1
Jun 13 '19 at 10:33
source share

It includes one more step, but a more flexible solution that will also work for 2d arrays and more complex filters is to create a boolean mask and then use the .sum () mask in the mask.

 >>>>y = np.array([0, 0, 0, 1, 0, 1, 1, 0, 0, 0, 0, 1]) >>>>mask = y == 0 >>>>mask.sum() 8 
0
Dec 24 '15 at 22:35
source share

If you do not want to use numpy or the collections module, you can use a dictionary:

 d = dict() a = [0, 0, 0, 1, 0, 1, 1, 0, 0, 0, 0, 1] for item in a: try: d[item]+=1 except KeyError: d[item]=1 

result:

 >>>d {0: 8, 1: 4} 

Of course, you can also use the if / else statement. I think the Counter function works almost the same, but it is more transparent.

0
Jul 08 '16 at 14:41
source share

For shared entries:

 x = np.array([11, 2, 3, 5, 3, 2, 16, 10, 10, 3, 11, 4, 5, 16, 3, 11, 4]) n = {i:len([j for j in np.where(x==i)[0]]) for i in set(x)} ix = {i:[j for j in np.where(x==i)[0]] for i in set(x)} 

Output the invoice:

 {2: 2, 3: 4, 4: 2, 5: 2, 10: 2, 11: 3, 16: 2} 

And indexes:

 {2: [1, 5], 3: [2, 4, 9, 14], 4: [11, 16], 5: [3, 12], 10: [7, 8], 11: [0, 10, 15], 16: [6, 13]} 
0
Nov 06 '18 at 11:12
source share
 dict(zip(*numpy.unique(y, return_counts=True))) 

Just copied a comment by Seppo Enarvi who deserves the correct answer

0
Sep 17 '19 at 10:48
source share

Numpy has a module for this. Just a little hack. Put your input array in the form of baskets.

 numpy.histogram(y, bins=y) 

The output is 2 arrays. One with the values ​​themselves, others with the corresponding frequencies.

-one
Apr 26 '17 at 10:37
source share
 using numpy.count $ a = [0, 0, 0, 1, 0, 1, 1, 0, 0, 0, 0, 1] $ np.count(a, 1) 
-one
Aug 19 '19 at 9:45
source share

wtf kkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkk

-3
May 10 '19 at 9:02
source share



All Articles