The class weight calculator gives the balanced class weights for training on imbalanced data, the imbalance ratio and the positive weight used in binary classification.
How the calculation works
- Enter the number of examples in each class, and 0 for the third class if there are only two.
- The calculator gives the balanced weight of each class, the imbalance ratio, the positive weight, the share of the smallest class and the total.
Example
A data set has 900, 80 and 20 examples in three classes, 1,000 in total. The balanced weights are 1,000 / (3 × 900) = 0.370, 1,000 / (3 × 80) = 4.167 and 1,000 / (3 × 20) = 16.667. The imbalance ratio is 45.0 to 1 and the smallest class is 2.00 % of the data. For the first two classes alone, the positive weight is 900 / 80 = 11.25.
Why it matters
The calculator helps data science students and practitioners who train classifiers on rare events such as fraud, defects or diseases, and who need weights for their loss function.
Good to know
Class weights are passed to the training function of most machine learning libraries. Very large weights make training unstable, so cap them or combine them with resampling. Oversampling the rare class duplicates examples and can overfit, while undersampling the common class throws information away. Methods such as generating synthetic examples exist too. Never rebalance the validation or test sets: they must keep the real class proportions so that the metrics reflect real use. Accuracy is misleading on imbalanced data, since a model that always predicts the majority class is right 90 % of the time here.
Related tools
The confusion matrix calculator evaluates classifiers, the sensitivity and specificity calculator covers test performance, the Matthews correlation coefficient calculator gives a robust single score and the design effect calculator handles weighting in surveys.