# How to handle imbalanced classes

**URL:** <https://discuss.pytorch.org/t/how-to-handle-imbalanced-classes/11264>\
**Category:** Uncategorized\
**Created:** [December 17, 2017, 5:23pm UTC](https://discuss.pytorch.org/t/how-to-handle-imbalanced-classes/11264 "2017-12-17T17:23:00Z")\
**Posts on this page:** 1\
**Showing post:** 2

<div class="post-metadata">

**Author:** ![ptrblck](https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/ptrblck/32/1823_2.png) [@ptrblck](https://discuss.pytorch.org/u/ptrblck)\
**Post date:** [December 17, 2017, 8:04pm UTC](https://discuss.pytorch.org/t/how-to-handle-imbalanced-classes/11264/2 "2017-12-17T20:04:30Z")

</div>

What kind of error do you get?

Here is a sample code, which should work fine:

```
numDataPoints = 1000
data_dim = 5
bs = 100

# Create dummy data with class imbalance 9 to 1
data = torch.FloatTensor(numDataPoints, data_dim)
target = np.hstack((np.zeros(int(numDataPoints * 0.9), dtype=np.int32),
                    np.ones(int(numDataPoints * 0.1), dtype=np.int32)))

print 'target train 0/1: {}/{}'.format(
    len(np.where(target == 0)[0]), len(np.where(target == 1)[0]))

class_sample_count = np.array(
    [len(np.where(target == t)[0]) for t in np.unique(target)])
weight = 1. / class_sample_count
samples_weight = np.array([weight[t] for t in target])

samples_weight = torch.from_numpy(samples_weight)
samples_weigth = samples_weight.double()
sampler = WeightedRandomSampler(samples_weight, len(samples_weight))

target = torch.from_numpy(target).long()
train_dataset = torch.utils.data.TensorDataset(data, target)

train_loader = DataLoader(
    train_dataset, batch_size=bs, num_workers=1, sampler=sampler)

for i, (data, target) in enumerate(train_loader):
    print "batch index {}, 0/1: {}/{}".format(
        i,
        len(np.where(target.numpy() == 0)[0]),
        len(np.where(target.numpy() == 1)[0]))
```

---

_[View the full topic](https://discuss.pytorch.org/t/how-to-handle-imbalanced-classes/11264)._
