How to handle imbalanced classes

The weights should be defined as the inverse class frequency defined for each sample.
Here is a small example. Could you try that or check for differences with your code?