Count the occurrence of a certain item in an ndarray - Numpy
Last Updated :
28 Nov, 2022
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
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.
Python3
import numpy as np
arr = np.array([2, 3, 4, 5, 3, 3,
5, 4, 7, 8, 3])
print('Numpy Array:')
print(arr)
c = 0
element = 3
for j in arr:
if j == element:
c += 1
print("element occurred", c, "times")
Output:
Numpy Array:[2 3 4 5 3 3 5 4 7 8 3]
element occurred 4 times
Count the occurrence of a certain item in an array using count_nonzero()
Here we are using the count_nonzero() function to count the occurrence of an item in the array if the match the target value.
Example 1:
For 1D array
Python3
import numpy as np
a = np.array([2, 3, 4, 5, 3, 3, 5, 4, 7, 8, 3])
print('Numpy Array:')
print(a)
# Count element '3' in numpy array
c = np.count_nonzero(a == 3)
print('Total occurrences of "3" in array: ', c)
Output:
Numpy Array: [2 3 4 5 3 3 5 4 7 8 3]
Total occurrences of "3" in array: 4
Example 2:
For 2D array
Python3
import numpy as np
a = np.array([[1, 3, 6],
[1, 3, 4],
[5, 3, 6],
[4, 7, 8],
[3, 6, 1]])
print('Numpy Array:')
print(a)
# Count '3' in numpy array
c = np.count_nonzero(a == 3)
print('Occurrences of "3" in array: ', c)
Output:
Numpy Array:
[[1 3 6]
[1 3 4]
[5 3 6]
[4 7 8]
[3 6 1]]
Occurrences of "3" in array: 4
Count the occurrence of a certain item in an array using sum()
A True is equivalent to 1 in Python, so usually add the True or non-zero values in the array to get the sum of values in the array that matches the condition.
Python3
import numpy as np
arr = np.array([2, 3, 4, 5, 3, 3, 5, 4, 7, 8, 3])
print('Numpy Array:')
print(arr)
# Count '3' in array
count = (arr == 3).sum()
print('Occurrences of "3" in array: ', count)
Output:
Numpy Array:
[2 3 4 5 3 3 5 4 7 8 3]
Occurrences of "3" in array: 4
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 array using the tolist()
Here we are converting the Numpy array into a list using the tolist() and then counting the number of elements that match the target elements.
Python3
import numpy as np
a = np.array([2, 3, 4, 5, 3, 3,
5, 4, 7, 8, 3])
# Count element '3' in numpy array
c = a.tolist().count(5)
print('Occurrences of "3" in array: ', c)
Output:
Occurrences of "3" in array: 2
Similar Reads
Find the number of occurrences of a sequence in a NumPy array The sequence is consisting of some elements in the form of a list and we have to find the number of occurrences of that sequence in a given NumPy array. This can be done easily by checking the sequence for every iteration of ndarray. But this leads to higher time so we use the concept of NumPy metho
1 min read
How to count the frequency of unique values in NumPy array? Let's see How to count the frequency of unique values in the NumPy array. Pythonâs Numpy library provides a numpy.unique() function to find the unique elements and their corresponding frequency in a NumPy array. numpy.unique() Syntax Syntax: numpy.unique(arr, return_counts=False) Return: Sorted uniq
4 min read
Python - Find all elements count in list In Python, counting the occurrences of all elements in a list is to determine how many times each unique element appears in the list. In this article, we will explore different methods to achieve this. The collections.Counter class is specifically designed for counting hashable objects. It provides
3 min read
numpy.ma.MaskedArray.count() function - Python numpy.ma.MaskedArray.count() function count the non-masked elements of the array along the given axis. Syntax : numpy.ma.MaskedArray.count(self, axis=None, keepdims = no value) Parameters : axis : [None or int or tuple of ints, optional] Axis along which the count is performed. The default axis is N
2 min read
NumPy ndarray.size() Method | Get Number of Elements in NumPy Array The ndarray.size() method returns the number of elements in the NumPy array. It works the same as np.prod(a.shape), i.e., the product of the dimensions of the array. Example Python3 import numpy as np arr = np.zeros((3, 4, 2), dtype = np.complex128) gfg = arr.size print (gfg) Output : 24Syntax Synta
1 min read
Numpy count_nonzero method | Python numpy.count_nonzero() function counts the number of non-zero values in the array arr. Syntax : numpy.count_nonzero(arr, axis=None) Parameters : arr : [array_like] The array for which to count non-zeros. axis : [int or tuple, optional] Axis or tuple of axes along which to count non-zeros. Default is
1 min read