Keras what does class_weight actually try to balance?
ML System Design practice on Codemia
Design recommenders, ranking systems and training pipelines the way ML interviews actually ask for them, with worked solutions.
Introduction
In Keras, class_weight does not try to balance the raw number of samples inside each batch. What it actually balances is each class's contribution to the training loss by multiplying the loss of every example according to that example's true class.
What class_weight Changes
Suppose you have a binary problem with many more negatives than positives. Without weighting, the loss is dominated by the majority class simply because there are more of those examples.
When you pass:
Keras treats an error on class 1 as five times more costly than an error on class 0.
Conceptually, the batch loss becomes:
So class_weight balances the optimization pressure, not the literal dataset counts.
A Small Example
Here is a minimal Keras training call:
If y_train contains class 1, those samples contribute a larger weighted loss during training. The optimizer therefore pays more attention to mistakes on that class.
Why This Helps with Imbalanced Data
Imagine a fraud dataset where:
- class
0means normal transaction - class
1means fraud - fraud examples are rare
A model can achieve high accuracy by predicting class 0 almost all the time. class_weight pushes back against that by making misclassified fraud examples more expensive.
This often improves minority-class recall, though sometimes at the cost of more false positives.
It Does Not Change the Labels or the Decision Rule
class_weight does not:
- duplicate minority samples
- change the class labels
- force balanced predictions at inference time
- automatically change the prediction threshold
It only changes how training loss is computed.
For example, if your final layer is sigmoid and you classify with a threshold of 0.5, class_weight does not change that threshold by itself. It may change the learned model enough that the score distribution moves, but the threshold logic is still your responsibility.
Compare class_weight and sample_weight
class_weight applies one weight per class. sample_weight is more general: it lets you weight individual rows.
Example:
Use class_weight when the weighting rule is "all samples of class c should count more or less." Use sample_weight when importance varies per example.
Choosing Reasonable Weights
A common heuristic is inverse frequency. If class 1 is much rarer than class 0, give class 1 a larger weight. For example:
This is only a starting point. The best weights depend on business cost, model behavior, and evaluation metrics.
What Metric Should Improve
Do not judge class_weight only by accuracy. On imbalanced tasks, accuracy can stay high while the minority class is still ignored.
Better metrics often include:
- recall for the minority class
- precision-recall tradeoff
- F1 score
- ROC AUC or PR AUC
If class weighting raises minority recall but destroys precision, you may need milder weights, a different threshold, or different data handling.
Common Pitfalls
The biggest mistake is expecting class_weight to "balance the dataset." It does not resample the training set. It changes the loss landscape so the optimizer cares more about certain classes.
Another issue is using extremely large weights without checking training stability. Very large weights can make optimization noisy and can overcorrect toward the minority class.
Finally, remember that class_weight is not a substitute for good data. If the minority class is mislabeled, too small, or not representative, weighting alone will not rescue the model.
Summary
- '
class_weightmultiplies the loss for samples based on their true class.' - It balances each class's influence on training, not the raw sample counts directly.
- It is useful for imbalanced classification when minority-class mistakes should matter more.
- It does not duplicate data, change labels, or pick a prediction threshold for you.
- Evaluate the effect with imbalance-aware metrics, not accuracy alone.
Related reading
- keras what is the difference between model.predict and model.predict_proba
- Keras why does entire epoch take longer time when it shows all batches are complete?
- Keras with tensorflow-gpu totally freezes PC
- Keras with TensorFlow backend not using GPU
- Keras,models.add missing 1 required positional argument ''layer''
- KerasRegressor Coefficient of Determination R2 `Score`
- Keras with Tensorflow Use memory as it's needed ResourceExhaustedError
- Keras/Tensorflow Combined \`Loss\` function for single output
.png&w=3840&q=75)
Tackling System Design Interview Problems
A short course that equips you with the skills to approach system design interviews methodically.
Start the free courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
ML System Design practice on Codemia
Design recommenders, ranking systems and training pipelines the way ML interviews actually ask for them, with worked solutions.