How can I assign a class_weight in Keras in a simple way?
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
Introduction
In Keras, the simplest way to use class weighting is to pass a dictionary to model.fit. Each class index maps to a multiplier that changes how much mistakes on that class contribute to the loss during training.
The Basic Pattern
For a binary problem with many more negatives than positives:
In this example, errors on class 1 count nine times as much as errors on class 0. That encourages the model to pay more attention to the minority class.
When class_weight Helps
class_weight is useful when:
- the dataset is imbalanced
- the minority class matters more than raw accuracy suggests
- you want a quick correction without changing the dataset itself
It is especially common in fraud detection, anomaly detection, medical screening, and moderation problems where rare positives matter a lot.
The important point is that class weights change the loss, not the labels or the model architecture.
Computing Weights Automatically
You do not have to invent the numbers manually. A common approach is to compute them from label frequency:
Then pass the resulting dictionary into fit:
This is usually the easiest reliable starting point for single-label classification tasks.
Multi-Class Example
The same idea works for more than two classes:
Each key is the integer class index, and each value is the loss multiplier for that class.
This is easiest when your labels are integer encoded. If your setup is more exotic, such as multi-label classification or sample-level weighting rules, sample_weight is often a better fit than class_weight.
class_weight vs. sample_weight
These two options solve related but different problems:
- '
class_weightsays "all examples of classkshould count more or less"' - '
sample_weightsays "this specific training example should count more or less"'
If every positive example should have the same extra importance, class_weight is clean and simple.
If weighting depends on row-specific metadata, confidence, or custom business logic, use sample_weight instead.
Keep Evaluation Separate from Training Weighting
A common mistake is assuming class weighting magically fixes evaluation. It only changes training loss. You still need to inspect metrics that reflect imbalance properly, such as:
- precision
- recall
- F1 score
- area under the precision-recall curve
Raw accuracy can remain misleading even after weighted training.
It is also common to retune the classification threshold after training. A weighted model may produce a better ranking of examples, but the threshold for converting probabilities into labels may still need adjustment.
Common Pitfalls
- Passing string labels or unexpected class IDs when Keras expects weights keyed by class index.
- Using
class_weightfor multi-label problems wheresample_weightis a better tool. - Assuming the automatically computed weights are always optimal. They are a starting point, not a law.
- Judging success only by accuracy on an imbalanced dataset.
- Setting extremely large weights and then being surprised by unstable training.
Summary
- In Keras, pass
class_weight={class_id: weight}tomodel.fit. - Class weights change how strongly each class contributes to the training loss.
- '
compute_class_weightfrom scikit-learn is a simple way to generate a starting dictionary.' - This works best for standard single-label classification with integer class IDs.
- Use proper imbalance-aware metrics to judge whether the weighting actually helped.

