Count occurrences of a value in NumPy array in Python

In this article, we will discuss different ways to count the occurrences of a value in numpy array.

Table of Contents

Use count_nonzero() to count occurrences of a value in a NumPy array

In Python, the numpy module provides a function count_nonzero(arr, axis=None), which returns the count of non zero values in a given numpy array. When the value of axis argument is None, then it returns the count
of non zero values in complete array. But in case you are dealing with multi-dimensional array, then you can use the axis argument to count occurrences of a value along the given axis.

Let’s understand by some examples,

Count all occurrences of value ‘3’ in a numpy array

When we applied a condition to the numpy array like, arr==3, then it applies the condition on each element of the array and stores the result as bool value in a new array. So,

arr==3

Returns a bool array of same size as arr,

[2 3 4 5 3 4 5 3 5 4 7 8 3 6 2]

This bool array contains True values at the indexes where value is 3 in the original array arr and False where value is not 3.

Now, if we count the True (non zero) values in this array, then we can get the count of value ‘3’ in the array.

import numpy as np

arr = np.array([2, 3, 4, 5, 3, 4, 5, 3, 5, 4, 7, 8, 3, 6, 2])


print('Numpy Array:')
print(arr)

# Count occurrence of element '3' in numpy array
count = np.count_nonzero(arr == 3)

print('Total occurences of "3" in array: ', count)

Output:

Numpy Array:
[2 3 4 5 3 4 5 3 5 4 7 8 3 6 2]
Total occurences of "3" in array:  4

To get the count we used the count_nonzero() function.

Use sum() to count occurrences of a value in a NumPy array

Similar to above solution we can apply a condition to numpy array to convert it to a bool array. A bool True is equivalent to 1 in python, so we can add add the True values in array to get the sum of values in array that matches the condition. Let’s use this logic to count all occurrences of value ‘3’ in numpy array,

import numpy as np

arr = np.array([2, 3, 4, 5, 3, 4, 5, 3, 5, 4, 7, 8, 3, 6, 2])

print('Numpy Array:')
print(arr)

# Count occurrence of element '3' in numpy array
count = (arr == 3).sum()

print('Total occurences of "3" in array: ', count)

Output:

Numpy Array:
[2 3 4 5 3 4 5 3 5 4 7 8 3 6 2]
Total occurences of "3" in array:  4

It returned the count of all occurrences of 3 in the array.

Use bincount() to count occurrences of a value in a NumPy array

In python, the numpy module provides a function numpy.bincount(arr), which returns a count of number of occurrences of each value in array of non-negative ints.

Let’s use this to count all occurrences of value ‘3’ in numpy array,

import numpy as np

arr = np.array([2, 3, 4, 5, 3, 4, 5, 3, 5, 4, 7, 8, 3, 6, 2])

count_arr = np.bincount(arr)

# Count occurrence of element '3' in numpy array
print('Total occurences of "3" in array: ', count_arr[3])

# Count occurrence of element '5' in numpy array
print('Total occurences of "5" in array: ', count_arr[5])

Output:

Total occurences of "3" in array:  4
Total occurences of "5" in array:  3

It returned the count of all occurences of 3 in the array.

How did it work?

bincount(arr), returned an array, where ith element contains the occurence of i in arr. For example,

  • result[1] contains the occurrences of 1 in array
  • result[2] contains the occurrences of 2 in array
  • result[3] contains the occurrences of 3 in array

Convert numpy array to list and count occurrences of a value in a array

We can convert the numpy array to a list and then use the count() function of list to get the count of occurrences of an element in it. For example,

import numpy as np

arr = np.array([2, 3, 4, 5, 3, 4, 5, 3, 5, 4, 7, 8, 3, 6, 2])

# Count occurrence of element '3' in numpy array
count = arr.tolist().count(3)

print('Total occurences of "3" in array: ', count)

Output:

Total occurences of "3" in array:  4

It returned the count of all occurences of 3 in the array.

Select elements from array that matches the value and count them

We can select only those elements from numpy array which are equal to given value and then we can can get the length of this new array. It will gives the count of occurrences of the value in original array. For example,

import numpy as np

arr = np.array([2, 3, 4, 5, 3, 4, 5, 3, 5, 4, 7, 8, 3, 6, 2])

# Count occurrence of element '3' in numpy array
count = arr[arr==3].shape[0]

print('Total occurences of "3" in array: ', count)

Output:

Total occurences of "3" in array:  4

It returned the count of all occurences of 3 in the array.

How did it work?

When we applied a condition to the numpy array like, arr==3, then it applies the condition on each element of the array and stores the result as bool value in a new array. Finally returns a bool array of same size as arr. It contains True where value is 3 in array and False where value is not 3. If we pass the bool array to subscript operator [] of numpy array then, it will select elements from array where bool value is True.

It means arr[arr==3], returned an array of 3’s only. Then we checked its length using the shape attribute.

Count occurrences of a value in 2D NumPy Array

To count the occurrences of a value in complete 2D Numpy array or Matrix we can use the count_nonzero() function with axis parameter as None. For example,

import numpy as np

# Create a 2D Numpy Array from list of lists
matrix = np.array( [[2, 3, 4],
                    [5, 3, 4],
                    [5, 3, 5],
                    [4, 7, 8],
                    [3, 6, 2]] )

# Count occurrence of element '3' in complete 2D Numpy Array
count = np.count_nonzero(matrix == 3)

print('Total occurrences of "3" in 2D array:')
print(count)

Output:

Total occurrences of "3" in 2D array: 4

Count occurrences of a value in each row of 2D NumPy Array

To count the occurrences of a value in each row of the 2D NumPy array pass the axis value as 1 in the count_nonzero() function. It will return an array containing the count of occurrences of a value in each row. For example,

import numpy as np

# Create a 2D Numpy Array from list of lists
matrix = np.array( [[2, 3, 4],
                    [5, 3, 4],
                    [5, 3, 5],
                    [4, 7, 8],
                    [3, 6, 2]] )

# Count occurrence of element '3' in each row
count = np.count_nonzero(matrix == 3, axis=1)

print('Total occurrences  of "3" in each row of 2D array: ', count)

Output:

Total occurrences of "3" in each row of 2D array: [1 1 1 0 1]

Count occurrences of a value in each column of 2D NumPy Array

To count the occurrences of a value in each column of the 2D NumPy array pass the axis value as 0 in the count_nonzero() function. It will return an array containing the count of occurrences of a value in each column. For example,

import numpy as np

# Create a 2D Numpy Array from list of lists
matrix = np.array( [[2, 3, 4],
                    [5, 3, 4],
                    [5, 3, 5],
                    [4, 7, 8],
                    [3, 6, 2]] )

# Count occurrence of element '3' in each column
count = np.count_nonzero(matrix == 3, axis=0)

print('Total occurrences  of "3" in each column of 2D array: ', count)

Output:

Total occurrences of "3" in each column of 2D array: [1 3 0]
Advertisements

Leave a Comment

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Scroll to Top