Counting the matching value to count the occurrence of an item

Here we are using the concept of matching the value present in the array and finding the occurrence of that element in the array. 

Python3




import numpy as np
 
a = np.array([2, 3, 4, 5, 3, 3,
              5, 4, 7, 8, 3])
 
# Count '3' in numpy array
c = a[a == 3].shape[0]
 
print('Occurrences of "3" in array is: ', c)


Output:

Occurrences of "3" in array is: 4

Count the occurrence of a certain item in an ndarray – Numpy

In this article, the task is to find out how to count the occurrence of a certain item in an nd-Array in Python.

Example

Array = [[ 0  1  2  3]
        [ 4  5  2  7]  
        [ 8  2 10 11]]
        
Input: element = 2

Output: 3 times

Similar Reads

Count the occurrence of a certain item in an array using a loop

Here we are using a loop to count the occurrence of an element in an array....

Count the occurrence of a certain item in an array using count_nonzero()

...

Count the occurrence of a certain item in an array using sum()

Here we are using the count_nonzero() function to count the occurrence of an item in the array if the match the target value....

Counting the matching value to count the occurrence of an item

...

Count the occurrence of a certain item in an array using the tolist()

...