Class Weight Calculator
Enter class labels and sample counts to compute balanced class weights for imbalanced datasets — scikit-learn compatible output.
Use the Class Weight Calculator
Dataset Classes
Enter each class label and the number of samples it contains.
Computed Weights
Enter class data and click Calculate.
| Class | Samples | Share | Weight |
|---|
scikit-learn dict
NumPy / PyTorch tensor
Formula:
wc = n_samples / (n_classes × n_samples_c)
Copied!
Summary
Class imbalance — where one label has far more samples than others — causes classifiers to predict the majority class and ignore minorities. Balanced class weights counteract this by scaling each class's loss contribution inversely to its frequency, so rare classes get proportionally more attention during training. This tool computes those weights using the same formula as scikit-learn's <code>class_weight="balanced"</code>: <strong>w<sub>c</sub> = n_samples / (n_classes × n_samples_c)</strong>.
How it works
- Enter a class label and its sample count for each class in your dataset.
- Add as many classes as needed with the "Add Class" button.
- The calculator applies the scikit-learn balanced formula: w = n_total / (n_classes × n_c).
- Results appear as a weight dictionary you can paste directly into your training code.
- Higher weights indicate rarer classes that the model should prioritize.
Use cases
- Binary fraud detection where fraud events are less than 1% of samples.
- Medical diagnosis models with rare positive diagnoses.
- Multi-class NLP classifiers with unequal label distributions.
- Image classification where some categories have fewer photos.
- Any sklearn Pipeline using LogisticRegression, SVC, or RandomForestClassifier.
- Computing sample_weight arrays for custom PyTorch or TensorFlow loss functions.
Frequently Asked Questions
Last updated: 2026-06-11 · Reviewed by Nham Vu