1

I have a 2D array of values, and would like to count the occurrences of elements in each row. The bincount function only works on 1D arrays, and using python loops to loop through every row would not be ideal. The unique function has an argument return_counts, however, it counts entire columns, as opposed to the element by element basis for each row which I am looking for. To illustrate this more clearly:

import numpy as np
a = np.array([[1, 1, 3, 2, 2, 2],
              [2, 3, 4, 3, 5, 3]])
print(a)
# [[1, 1, 3, 2, 2, 2],
#  [2, 3, 4, 3, 5, 3]]

b, c = np.unique(a, return_counts=True, axis=1) # Count elements in axis 1

print(b)
# [[1 1 2 2 3]
#  [2 3 3 5 4]]

print(c)
# [1 1 2 1 1]

Instead of counting elements in each row, unique counts the columns along the horizontal axis 1 which I specified; the column [2, 3] is counted twice while the others are counted once. Is there any way to achieve the behaviour I want using native numpy functions instead of python loops?

EnderShadow8
  • 788
  • 6
  • 17

1 Answers1

0

I don't believe numpy has a good counter function. I suggest going with collections.Counter(), but it does add a for loop on the rows.

from collections import Counter
counts = [Counter(a[k, :]) for k in range(a.shape[0])]

The Counter object can be accessed with different methods, for instance .most_common():

counts[0].most_common()
Out: [(2, 3), (1, 2), (3, 1)]
Mathieu
  • 5,410
  • 6
  • 28
  • 55