Wednesday, 4 October 2023

How to find a number of occurrences of every element of a numpy array?

Given an array of integers, I would like to obtain an array of the same size where every value is a number of occurrences of a corresponding element in the original array.

For example given the following array:

a = np.array([1, 1, 4, 10, 5, 3, 5, 5, 8, 9])

This should be the result:

array([2, 2, 1, 1, 3, 1, 3, 3, 1, 1])

Even though it is straightforward to achieve this via collections.Counter or built-in list.count(), I'm looking for a more performant way to work with large lists.



from How to find a number of occurrences of every element of a numpy array?

No comments:

Post a Comment