Can't you just use `bincount` directly here if your values are numbers from `0 to n_classes - 1`?