Keras
machine learning
class weight
model training
imbalance handling

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.

Practice ML system design

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:

python
class_weight = {0: 1.0, 1: 5.0}

Keras treats an error on class 1 as five times more costly than an error on class 0.

Conceptually, the batch loss becomes:

text
weighted_loss = weight_of_true_class * sample_loss

So class_weight balances the optimization pressure, not the literal dataset counts.

A Small Example

Here is a minimal Keras training call:

python
1model.fit(
2    x_train,
3    y_train,
4    epochs=5,
5    class_weight={0: 1.0, 1: 5.0}
6)

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 0 means normal transaction
  • class 1 means 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:

python
1model.fit(
2    x_train,
3    y_train,
4    sample_weight=[1.0, 1.0, 3.0, 1.0, 0.5]
5)

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:

python
1from collections import Counter
2
3y_train = [0, 0, 0, 0, 1, 1]
4counts = Counter(y_train)
5total = len(y_train)
6
7class_weight = {
8    cls: total / (len(counts) * count)
9    for cls, count in counts.items()
10}
11
12print(class_weight)

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_weight multiplies 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
Free course
Beginner
7 lessons
2 hours
Tackling System Design Interview Problems

A short course that equips you with the skills to approach system design interviews methodically.

Start the free course
Track 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.

Practice ML system design

All Rights Reserved.