Using numpy.unique:

Use with AIimport numpy
a = numpy.array([0, 3, 0, 1, 0, 1, 2, 1, 0, 0, 0, 0, 1, 3, 4])
unique, counts = numpy.unique(a, return_counts=True)

>>> dict(zip(unique, counts))
{0: 7, 1: 4, 2: 1, 3: 2, 4: 1}

Non-numpy method using collections.Counter;

Use with AIimport collections, numpy
a = numpy.array([0, 3, 0, 1, 0, 1, 2, 1, 0, 0, 0, 0, 1, 3, 4])
counter = collections.Counter(a)

>>> counter
Counter({0: 7, 1: 4, 3: 2, 2: 1, 4: 1})
Answer from Ozgur Vatansever on Stack Overflow
🌐
Statology
statology.org › home › how to count occurrences of elements in numpy
How to Count Occurrences of Elements in NumPy
December 6, 2021 - From the output we can see that 7 values in the NumPy array have a value less than 6. The following code shows how to count the number of elements in the NumPy array that are equal to 2 or 7:
🌐
Note.nkmk.me
note.nkmk.me › home › python › numpy
NumPy: Count values in an array with conditions | note.nkmk.me
February 3, 2024 - This article explains how to count values in a NumPy array (ndarray) that meet certain conditions. Count values in an array with a condition: np.count_nonzero() Count values row-wise or column-wise Ch ...
🌐
GeeksforGeeks
geeksforgeeks.org › python › numpy-size-function-python
Numpy size() function | Python - GeeksforGeeks
July 12, 2025 - The numpy.size() function is a tool to understand how many elements exist in your array whether it's one-dimensional or multi-dimensional.
🌐
pythontutorials
pythontutorials.net › blog › numpy-count
Mastering `numpy.count` - A Comprehensive Guide — pythontutorials.net
`NumPy` is a fundamental library in Python for scientific computing. It provides a high-performance multidimensional array object and tools for working with these arrays. While there isn't a direct `numpy.count` function, we often refer to ways of counting elements in `NumPy` arrays based on certain conditions.
🌐
Python Guides
pythonguides.com › python-numpy-count
np.count_nonzero() Function in NumPy
May 16, 2025 - np.count_nonzero() is a NumPy function that counts the number of non-zero elements in an array.
Find elsewhere
🌐
GeeksforGeeks
geeksforgeeks.org › numpy › count-the-occurrence-of-a-certain-item-in-an-ndarray-numpy
Count the occurrence of a certain item in an ndarray - Numpy - GeeksforGeeks
July 23, 2025 - 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)
🌐
Sling Academy
slingacademy.com › home › numpy › how to count elements/rows/columns of a numpy ndarray
How to count elements/rows/columns of a NumPy ndarray - Sling Academy
January 23, 2024 - When working with data, one may also wish to count the unique elements. NumPy provides the np.unique() function for this purpose:
🌐
Arab Psychology
scales.arabpsychology.com › home › how to easily count element occurrences in numpy arrays
How To Easily Count Element Occurrences In NumPy Arrays
December 2, 2025 - Since NumPy treats True as 1 and False as 0 in arithmetic operations, numpy.count_nonzero() simply sums the number of True values in this resultant boolean array, giving us the count of elements that satisfy the condition.
🌐
Nivedita's blog
nivdatta.hashnode.dev › count-an-element-in-numpy-array
Count an element in numpy array
July 3, 2021 - 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.
Top answer
1 of 3
1

In numpy you can use numpy.unique and then concatenate the unique elements with their counts.

import numpy as np
m = np.array([[0., 143.],
              [0., 170.],
              [0., 143.],
              [1., 269.],
              [0., 170.],
              [1., 269.],
              [0., 155.],
              [1., 269.]])
rows, counts = np.unique(m, axis=0, return_counts=True)
result = np.concatenate([rows, counts[:, None]], axis=1)

or equivalently

result = np.column_stack(np.unique(m, axis=0, return_counts=True))

which both result in

[[  0.  143.   2.]
 [  0.  155.   1.]
 [  0.  170.   2.]
 [  1.  269.   3.]]

Note that the output order of numpy.unique is not stable. If the order of the output needs to match the order of first occurrences then you can numpy.argsort the result with respect to the indices returned by numpy.unique.

rows, indices, counts = np.unique(m, axis=0, return_index=True, return_counts=True)
result = np.concatenate([rows, counts[:, None]], axis=1)[np.argsort(indices)]

which results in

[[  0.  143.   2.]
 [  0.  170.   2.]
 [  1.  269.   3.]
 [  0.  155.   1.]]
2 of 3
1

There is torch.bincount, but it "only supports 1-d non-negative integral inputs".

Anyway, if you are to count elements, due to float precision errors it is better to work with integers.

For the > 1-d case, you can create a custom function:

def count(x):
    unique_values = set(x)
    counts = [
        (
            tuple(v.tolist()),
            (x == v).all(axis=-1).sum().item()
        )
        for v in unique_values
    ]
    return dict(counts)

Which gives counts as a dictionary, similar to standard library Counter, that you can reformat later to suit your needs:

>>> count(matrix.to(int))
{(1, 269): 3, (0, 170): 2, (0, 143): 2, (0, 155): 1}
🌐
NumPy
numpy.org › doc › 2.2 › reference › generated › numpy.size.html
numpy.size — NumPy v2.2 Manual
numpy.size(a, axis=None)[source]# Return the number of elements along a given axis. Parameters: aarray_like · Input data. axisint, optional · Axis along which the elements are counted. By default, give the total number of elements. Returns: element_countint ·
🌐
NumPy
numpy.org › doc › 2.5 › reference › generated › numpy.size.html
numpy.size — NumPy v2.5 Manual
numpy.size(a, axis=None)[source]# Return the number of elements along a given axis. Parameters: aarray_like · Input data. axisNone or int or tuple of ints, optional · Axis or axes along which the elements are counted. By default, give the total number of elements.
🌐
IncludeHelp
includehelp.com › python › how-to-count-the-occurrence-of-a-certain-item-in-a-ndarray.aspx
Count the occurrence of all elements in a NumPy ndarray
To count the occurrence of all elements in a NumPy ndarray, we will use numpy.unique() method by passing an argument called return_counts=True and then create a dictionary of unique values and their count so that we can get the count of each element in a ndarray.