Torch/numpy: groupby pandas alternative
Is there some efficient way to rewrite the following code to avoid installing &amp; importing pandas and using torch/numpy instead? I am used to work with pandas, so I wrote it like this, but I am trying to learn numpy and torch, so I am looking for alternative solutions that do not use pandas.

bins = torch.LongTensor(3072).random_(0, 35)
weights = torch.rand((3072))
df = pd.DataFrame({'weights': weights.numpy(), 'bins': bins.numpy()})
bins_sum = df.groupby('bins').weights.sum().values


So, basically: how, without using pandas, get a sum of weights grouped by bins?

You can compute unique elements of bins via torch.unique (the values to group by) and then use index masks for accessing the corresponding elements in weights:

unique = torch.unique(bins)
result = torch.zeros(unique.size(), dtype=weights.dtype)
for i, val in enumerate(unique):
    result[i] += weights[bins == val].sum()

