If you are interested in the fastest execution, you know in advance which value(s) to look for, and your array is 1D, or you are otherwise interested in the result on the flattened array (in which case the input of the function should be np.ravel(arr) rather than just arr), then Numba is your friend:
import numba as nb @nb.jit def count_nb(arr, value): result = 0 for x in arr: if x == value: result += 1 return resultor, for very large arrays where parallelization may be beneficial:
@nb.jit(parallel=True) def count_nbp(arr, value): result = 0 for i in nb.prange(arr.size): if arr[i] == value: result += 1 return resultThese can be benchmarked against np.count_nonzero() (which also has a problem of creating a temporary array -- something that is avoided in the Numba solutions) and a np.unique()-based solution (which is actually counting all unique value values contrarily to the other solutions).
import numpy as np def count_np(arr, value): return np.count_nonzero(arr == value) import numpy as np def count_np_uniq(arr, value): uniques, counts = np.unique(a, return_counts=True) counter = dict(zip(uniques, counts)) return counter[value] if value in counter else 0Since the support for "typed" dicts in Numba, it is also possible to have a function counting all occurrences of all elements. This competes more directly with np.unique() because it is capable of counting all values in a single run. Here is proposed a version which eventually only returns the number of elements for a single value (for comparison purposes, similarly to what is done in count_np_uniq()):
@nb.jit def count_nb_dict(arr, value): counter = {arr[0]: 1} for x in arr: if x not in counter: counter[x] = 1 else: counter[x] += 1 return counter[value] if value in counter else 0The input is generated with:
def gen_input(n, a=0, b=100): return np.random.randint(a, b, n)The timings are reported in the following plots (the second row of plots is a zoom on the faster approaches):
Showing that the simple Numba-based solution is fastest for smaller inputs and the parallelized version is fastest for larger inputs. They NumPy version is reasonably fast at all scales.
When one wants to count all values in an array, np.unique() is more performant than a solution implemented manually with Numba for sufficiently large arrays.
EDIT: It seems that the NumPy solution has become faster in recent versions. In a previous iteration, the simple Numba solution was outperforming NumPy's approach for any input size.
Full code available here.
In this article, we will discuss different ways to count the occurrences of a value in numpy array. Table of Contents 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 Let’s understand by some examples, 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, Advertisements Returns a bool array of same size as arr, 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. Output: To get the count we used the
count_nonzero() function. 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,Use count_nonzero() to count occurrences of a value in a NumPy array
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. Count all occurrences of value ‘3’ in a numpy array
Output:
Numpy Array: [2 3 4 5 3 4 5 3 5 4 7 8 3 6 2] Total occurences of "3" in array: 4It 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: 3It 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: 4It 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: 4It 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: 4Count 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]