Skip to content
Omnikalc

Class weight calculator

Enter the number of examples in each class to get the balanced weights used to train on imbalanced data, the imbalance ratio and the positive weight for binary problems.

Updated on
Your inputs

Value out of range

Value out of range

Value out of range

Results

Balanced weight of class 0

0.370

Balanced weight of class 1

4.167

Balanced weight of class 2

16.667

Imbalance ratio (largest to smallest)
45.0 : 1
Strong imbalance: weights, resampling or other metrics are needed
Positive weight (class 0 over class 1)
11.25
Share of the smallest class
2.00 %
Total examples
1,000

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.

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.

Frequently asked questions

What is the formula for balanced weights?

The weight of a class is the total number of examples divided by the number of classes times the size of the class. It is the rule behind the balanced option in several libraries. The weights make each class contribute equally to the loss, and their weighted sum equals the number of examples.

What is the positive weight?

Binary loss functions often take a single ratio, the number of negatives divided by the number of positives. A positive example then counts that many times in the loss. Here the first class plays the negative role and the second the positive role.

Are weights always the answer?

No. Weights shift the decision boundary toward the rare class but can add noise, and predicted probabilities are no longer calibrated. Alternatives are resampling, a threshold chosen on a validation set, and metrics that suit imbalance such as precision, recall or the area under the precision-recall curve.

Was this calculator helpful?

Thanks for your feedback!

Results are estimates provided for information purposes. They do not replace professional advice.