Class Weight Calculator
Enter class labels and sample counts to compute balanced class weights for imbalanced datasets — scikit-learn compatible output.
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
Enter class labels and sample counts to compute balanced class weights for imbalanced datasets — scikit-learn compatible output.
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-07-22 ·
Reviewed by Nham Vu