# Why doesn't class weight resolve the imbalanced classification problem?

I know that in imbalanced classification, the classifier tends to predict all the test labels as larger class label, but if we use class weight in loss function, it would be reasonable to expect the problem to be solved. So why we need some approaches like down sampling or up sampling for imbalanced classification problem?

Class weights do help with the imbalance problem ("resolve" seems too much), but upsampling has a certain advantage on it.

If you think about it, downsampling/upsampling the number of samples in each class to balance the dataset is almost exactly the same as using class weights.

For example, say you have a dataset containing 3 samples divided into 2 classes: $[A_1,&space;A_2,&space;B_1]$ and you are training with an MSE loss.

You can choose to upsample the number of samples from class B, which will get you the following cost function over a single epoch:

$L&space;=&space;\frac{1}{N}&space;\sum&space;(y_{pred}-y_{true})^2$ $L&space;=&space;\frac{1}{4}((y_{pred1}-A_1)^2+(y_{pred2}-A_2)^2+(y_{pred3}-B_1)^2+(y_{pred4}-B_1)^2)$

Here is where the small difference comes into effect, if you are training in a batch gradient descend (a single weights update per epoch), the prediction for the 2 identical B1 samples will be the same, so the loss function can be written as:

$L&space;=&space;\frac{1}{4}((y_{pred1}-A_1)^2+(y_{pred2}-A_2)^2+2\cdot&space;(y_{pred3}-B_1)^2)$

Which is exactly the same as using a weighted loss function. However if you are using a mini-batch gradient descend (as most model our days), the 2 different samples may appear in different mini-batches, and so the predictions for them at the same epoch won't be the same (because one of them will pass thru a model that was already updated once).

This is a small difference but sometimes it is important. It means that with weighted classes, the effective learning rate varies between mini-batches. In some cases that can make learning unstable. So, when possible, upsampling is the better approach (practically it produces slightly better results).

The problem is that you can't always upsample/downsample without any worry. If we go back to our example but this time we have a dataset of 5 samples, divided into 2 classes:

$[A_1,&space;A_2,&space;A_3,&space;B_1,&space;B_2]$

Downsampling is problematic - which of the 3 A class samples do we ignore?

Upsampling is problematic - which of the 2 B class samples do we duplicate?

It can be solved by randomly choosing which samples to keep/ignore at each epoch, however with very big datasets, this can lead to slower processing... So the weighted classes is still a valid option.

• "It can be solved by randomly choosing which samples to keep/ignore at each epoch". How is that a solution? What does randomness do for us here? – fabiomaia Mar 26 at 15:44