There are several issues with your current approach. Firstly, np.bincount(x) will give you counts for each positive integer value of x starting at 0 and ending with max(x) :
print(np.bincount([1, 1, 3, 3, 3, 4])) # [0, 2, 0, 3, 1] # ie [count for 0, count for 1, count for 2, count for 3, count for 4]
Therefore, if not all indices in acc.flat indexed, the length of np.bincount(raveled) will be greater than the number of unique indices. Whatever you really want, these are counts only for places in acc.flat that are indexed at least once.
Secondly, what you want to do is assign the bin account to the corresponding indexes in acc.flat . Your np.resize call is to repeat parts of your binson array to make it the same size as acc.flat , then change it to the same shape as acc . This will not cause the recycle bin to be assigned to the correct locations in acc !
As I solved this problem, it would be to use np.unique instead of np.bincount and use it to return both unique indices and their corresponding ones to the account. They can then be used to assign the correct counts to the correct unique locations within acc :
import numpy as np
ali_m source share