Unbalanced data and weighted cross entropy
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
Introduction
When a classification dataset is highly imbalanced, ordinary cross-entropy often teaches the model to optimize for the majority class because that is where most of the examples are. Weighted cross-entropy addresses this by making mistakes on rarer classes count more heavily in the loss. It does not magically solve imbalance, but it is one of the most direct ways to tell the optimizer that minority-class errors matter more.
Why Imbalanced Data Causes Trouble
Suppose a binary dataset has 95% negatives and 5% positives. A model that predicts “negative” for every example can still achieve 95% accuracy while being useless for the positive class.
This is why class imbalance is dangerous:
- accuracy can look deceptively high
- the minority class can receive weak learning signals
- the model may learn the easiest majority-class shortcut
Weighted loss is one way to counter that pressure during training.
Standard vs Weighted Cross-Entropy
For binary classification, ordinary binary cross-entropy treats each example equally. Weighted cross-entropy changes that by multiplying the loss contribution differently depending on the true class.
Conceptually:
- errors on the minority class get a larger multiplier
- errors on the majority class get a smaller multiplier
In TensorFlow, a common binary example uses pos_weight.
Here, positive-label mistakes are weighted more heavily than negative-label mistakes.
Keras Class Weights for Training
In many Keras workflows, the easiest practical approach is to use class_weight during fit.
This changes the effective importance of each class during optimization without requiring you to write a custom loss manually.
Choosing the Weights
A common starting heuristic is to weight classes inversely to their frequency. For a binary dataset:
- more frequent class gets smaller weight
- less frequent class gets larger weight
A simple example with scikit-learn:
This gives you a reasonable baseline, but it is still a heuristic. The best weighting depends on the business cost of false negatives and false positives, not just the raw class counts.
Weighted Loss Is Not the Only Tool
Weighted cross-entropy is useful, but it is not the only way to handle imbalance. Other approaches include:
- oversampling the minority class
- undersampling the majority class
- focal loss for hard-example emphasis
- threshold tuning after training
- collecting better minority-class data
In many projects, the best result comes from combining weighted loss with evaluation metrics that actually reflect minority-class performance, such as precision, recall, PR AUC, or F1.
Be Careful with Metrics
If you use weighted loss but still judge success only by raw accuracy, you can misread what happened. Weighted loss changes optimization pressure, not the meaning of an uninformative metric.
For imbalanced problems, monitor metrics such as:
- recall on the minority class
- precision-recall tradeoff
- confusion matrix
- ROC AUC or PR AUC depending on the task
The model should be evaluated according to the error types the application actually cares about.
Common Pitfalls
The most common mistake is assuming weighted cross-entropy alone fixes every imbalance problem without checking recall, precision, or threshold behavior afterward. Another is choosing weights mechanically from class counts even when business costs suggest a different tradeoff. Developers also often combine sigmoid outputs with the wrong loss formulation, such as mixing logits-based weighted loss with already-activated probabilities. A final issue is leaving the decision threshold at 0.5 and then blaming the weighted loss when the post-training threshold is what really needs tuning.
Summary
- Weighted cross-entropy helps when class imbalance makes ordinary loss favor the majority class too strongly.
- It increases the optimization penalty for errors on underrepresented classes.
- In Keras,
class_weightis often the simplest practical way to apply the idea. - Weight selection should reflect both class frequency and business error costs.
- Weighted loss should be paired with minority-aware evaluation metrics and sensible threshold tuning.

