Count an element in numpy array

Here are the different ways we can count the occurrence of an element in a numpy array !

Count an element in numpy array

Here are the different ways you can count the occurrence of an element in a numpy array.

The non-numpy way

Here are the ways we can calculate the occurrence of various elements in a numpy array without using the inbuilt methods

Method 1: convert array to python list

>>> import numpy as np
>>> arr = np.array([1, 2, 2, 2, 2, 0, 2, 3, 3, 3, 0, 0, 2, 2, 0])
>>> lst = list(arr)
>>> lst.count(0)
4
>>> lst.count(2)
7
>>> lst.count(5)
0

Method 2: using collections.counter

>>> import collections, numpy
>>> arr = np.array([1, 2, 2, 2, 2, 0, 2, 3, 3, 3, 0, 0, 2, 2, 0])
>>> collections.Counter(arr)
Counter({2: 7, 0: 4, 3: 3, 1: 1})

Method 3: using dictionary comprehension

>>> import numpy as np
>>> arr = np.array([1, 2, 2, 2, 2, 0, 2, 3, 3, 3, 0, 0, 2, 2, 0])
>>> counts = {int(value): list(arr).count(value) for value in set(arr)}
>>> counts
{0: 4, 1: 1, 2: 7, 3: 3}

The numpy way

These are the different ways we can find occurrence of any element in a numpy element using the unbuilt methods.

Method 1: using count_nonzero

>>> import numpy as np
>>> arr = np.array([1, 2, 2, 2, 2, 0, 2, 3, 3, 3, 0, 0, 2, 2, 0])
>>> np.count_nonzero(arr == 0)
4
>>> np.count_nonzero(arr == 2)
7
>>> np.count_nonzero(arr == 5)
0

Method 2: using sum

>>> import numpy as np
>>> arr = np.array([1, 2, 2, 2, 2, 0, 2, 3, 3, 3, 0, 0, 2, 2, 0])
>>>
>>> (arr ==  0).sum()
4
>>> (arr ==  2).sum()
7
>>> (arr ==  5).sum()
0

Method 3: using unique

>>> import numpy as np
>>> arr = np.array([1, 2, 2, 2, 2, 0, 2, 3, 3, 3, 0, 0, 2, 2, 0])
>>>
>>> unique, counts = np.unique(arr, return_counts=True)
>>> dict(zip(unique, counts))
{0: 4, 1: 1, 2: 7, 3: 3}

Method 4: using where

>>> import numpy as np
>>> arr = np.array([1, 2, 2, 2, 2, 0, 2, 3, 3, 3, 0, 0, 2, 2, 0])
>>> len(np.where(arr==0.)[0])
4
>>> len(np.where(arr==2.)[0])
7
>>> len(np.where(arr==5.)[0])
0

Method 5: using bincount

>>> import numpy as np
>>> arr = np.array([1, 2, 2, 2, 2, 0, 2, 3, 3, 3, 0, 0, 2, 2, 0])
>>> np.bincount(arr, minlength=np.size(arr))
array([4, 1, 7, 3, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0])
>>>
>>> arr = np.array([1, 2, 2, 2, 2, 0, 2, 3, 3, 3, 0, 0, 2, 2, 100])
>>>
>>> np.bincount(arr, minlength=np.size(arr))
array([3, 1, 7, 3, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
       0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
       0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
       0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
       0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1])

The bincount method works only for integer values and outputs a list of count for each variable in the array. 0th index gives count of element 0, 5th index will give the count of element 5 and so on. In the second example I showed, as I added 100 to the list, the output array size increased to add the count for element 100.

While this might not be a feasible option in case of large integer values, it still is an interesting way to find the count of elements.

The Pandas way !

>>> import numpy as np
>>> import pandas as pd
>>>
>>> arr = np.array([1, 2, 2, 2, 2, 0, 2, 3, 3, 3, 0, 0, 2, 2, 100])
>>> pd.Series(arr).value_counts()
2      7
0      3
3      3
1      1
100    1
dtype: int64

What other ways can we count elements in a numpy array?

 
Share this