How to get occurrence count of item in array in-place
I have a array which I am trying to get weights for. It looks like this:
Target: tensor([ 1, 6, 5, 8, 10, 5, 4, 5, 10, 10, 9, 8, 10, 4, 10, 9])

I need to get the counts of each item in the array. So I would like it to return this:
Class count: [1, 1, 3, 2, 5, 3, 2, 3, 5, 5, 2, 2, 5, 2, 5, 2]

This is my code right now:
class_sample_count = np.unique(y, return_counts=True)[1]
weight = 1. / class_sample_count

print(f'Target: {y}, Class count: {class_sample_count}, Weight: {weight}')

samples_weight = weight[y]
weights = torch.from_numpy(samples_weight)

.. but the class count prints as this:
Class count: [1 2 3 1 2 2 5]

How can I get the counts of each value while keeping the length of the original array?

Use return_inverse of numpy.unique:
y = [ 1, 6, 5, 8, 10, 5, 4, 5, 10, 10, 9, 8, 10, 4, 10, 9]
uniq, inv, cnt = np.unique(y, return_inverse=True, return_counts=True)
cnt[inv]

Output:
array([1, 1, 3, 2, 5, 3, 2, 3, 5, 5, 2, 2, 5, 2, 5, 2])

