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?