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.

Copied!

Summary

Enter class labels and sample counts to compute balanced class weights for imbalanced datasets — scikit-learn compatible output.

How it works

  1. Enter a class label and its sample count for each class in your dataset.
  2. Add as many classes as needed with the "Add Class" button.
  3. The calculator applies the scikit-learn balanced formula: w = n_total / (n_classes × n_c).
  4. Results appear as a weight dictionary you can paste directly into your training code.
  5. 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