Keras
class_weight
machine learning
deep learning
tutorial

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:

python
1import numpy as np
2import tensorflow as tf
3
4x = np.random.randn(200, 10).astype("float32")
5y = np.array([0] * 180 + [1] * 20)
6
7model = tf.keras.Sequential(
8    [
9        tf.keras.layers.Dense(16, activation="relu"),
10        tf.keras.layers.Dense(1, activation="sigmoid"),
11    ]
12)
13
14model.compile(optimizer="adam", loss="binary_crossentropy", metrics=["accuracy"])
15
16history = model.fit(
17    x,
18    y,
19    epochs=3,
20    batch_size=32,
21    class_weight={0: 1.0, 1: 9.0},
22    verbose=0,
23)

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:

python
1import numpy as np
2from sklearn.utils.class_weight import compute_class_weight
3
4y = np.array([0] * 180 + [1] * 20)
5classes = np.unique(y)
6weights = compute_class_weight(class_weight="balanced", classes=classes, y=y)
7class_weight = dict(zip(classes, weights))
8
9print(class_weight)

Then pass the resulting dictionary into fit:

python
model.fit(x, y, class_weight=class_weight)

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:

python
1import numpy as np
2
3y = np.array([0] * 100 + [1] * 30 + [2] * 10)
4class_weight = {
5    0: 1.0,
6    1: 3.3,
7    2: 10.0,
8}

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_weight says "all examples of class k should count more or less"'
  • 'sample_weight says "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_weight for multi-label problems where sample_weight is 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} to model.fit.
  • Class weights change how strongly each class contributes to the training loss.
  • 'compute_class_weight from 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.

Course illustration
Course illustration

All Rights Reserved.