How to Use class_weight='balanced' for Imbalanced Classes in scikit-learn
When one class is rare, a classifier learns mostly from the common one, and misses many of the rare cases. Many scikit-learn classifiers take class_weight="balanced", which makes each class weigh the same in training, whatever its size. This guide trains the same model with and without it on data where 1 row in 10 is rare, and compares what it catches, with real output from scikit-learn 1.9.1.
Syntax
model = Classifier(class_weight="balanced")
| Part | What it means |
|---|---|
Classifier | A scikit-learn classifier that takes class_weight, such as LogisticRegression or HistGradientBoostingClassifier |
class_weight="balanced" | Weights every row by its class's rarity, so each class counts as much as the others in total |
The reference for LogisticRegression gives the formula: "The “balanced” mode uses the values of y to automatically adjust weights inversely proportional to class frequencies in the input data as n_samples / (n_classes * np.bincount(y))." The default, None, gives every row the same weight.
Step 1: Train With and Without It
The example makes 2,000 rows with make_classification, about 1 in 10 of them in the rare class 1, and trains a logistic regression both ways:

from sklearn.datasets import make_classification
from sklearn.linear_model import LogisticRegression
from sklearn.metrics import precision_score, recall_score
from sklearn.model_selection import train_test_split
# 2,000 made-up rows; about 1 in 10 belongs to the rare class, 1.
X, y = make_classification(n_samples=2000, weights=[0.9, 0.1], random_state=0)
X_train, X_test, y_train, y_test = train_test_split(X, y, stratify=y, random_state=0)
for class_weight in [None, "balanced"]:
model = LogisticRegression(class_weight=class_weight).fit(X_train, y_train)
predicted = model.predict(X_test)
print(
f"class_weight={class_weight!s:<9} "
f"recall={recall_score(y_test, predicted):.3f} "
f"precision={precision_score(y_test, predicted):.3f}"
)
uv run python class_weight.py
class_weight=None recall=0.923 precision=1.000
class_weight=balanced recall=0.942 precision=0.860

With "balanced", the model caught more of the rare class, recall 0.923 to 0.942, and paid for it with false alarms, precision 1.000 to 0.860. Giving the rare class more weight moves the model's answers towards it: more of the real cases found, more ordinary rows flagged by mistake.
Common Mistakes
| Mistake | What happens | Do this instead |
|---|---|---|
| Judging the result on accuracy alone | The extra false alarms lower accuracy, 0.992 to 0.978 here, and the gain on the rare class is hidden | Compare recall and precision, or balanced accuracy |
| Reading the weighted model's probabilities as true chances | The weights push its probabilities towards the rare class | Check its calibration curve before trusting them as chances |
Troubleshooting
| Symptom | Likely cause | Fix |
|---|---|---|
TypeError: GradientBoostingClassifier.__init__() got an unexpected keyword argument 'class_weight' | That estimator doesn't take class weights | Check its reference for class_weight, or use one that has it, such as HistGradientBoostingClassifier |
FAQ
Does class_weight change the data?
No: the rows stay the same, and each one counts more or less in the model's loss.
Do the probabilities change too?
Yes. A model trained with balanced weights gives the rare class higher probabilities than its real share: How to Plot a Calibration Curve in scikit-learn shows it on this kind of data.
Conclusion
class_weight="balanced" gave the rare class as much weight as the common one, and the model caught more rare cases, recall 0.942 against 0.923, at the cost of more false alarms, precision 0.860 against 1.000.
Additional Resources
- sklearn.linear_model.LogisticRegression — the
class_weightparameter and its formula.
