C.W.K.
Stream
Lesson 05 of 06 · published

Handling Class Imbalance

~8 min · data

Level 0Keras Apprentice
0 XP0/97 lessons0/20 achievements
0/120 XP to next level120 XP to go0% complete

Why imbalance breaks accuracy

When one class dominates — say 95% negative, 5% positive — a model can hit 95% accuracy by doing nothing useful: always guess "negative." The loss barely punishes it, because the rare class contributes so few examples to the gradient. This is the classic fraud/disease/defect-detection trap, and it's why accuracy is a liar on imbalanced data. You have to measure precision and recall on the minority class to see what's actually happening.

Three levers, in order of cost

Reach for these from cheapest to most involved. (1) class_weight — a dict passed to fit() that scales each class's loss contribution, so a mistake on the rare class hurts more. Free, one line, try it first. (2) sample_weight — a per-example weight array when you need finer control than per-class. (3) Resampling — oversample the minority (duplicate or synthesize with SMOTE) or undersample the majority, done in the data pipeline before fit() ever sees the data.

One more lever at inference

Even after weighting, you can tune the decision threshold. A binary classifier defaults to calling anything above 0.5 "positive," but on imbalanced data lowering that to, say, 0.3 trades precision for recall — useful when missing a positive (an undiagnosed disease) costs far more than a false alarm. Threshold tuning is post-hoc and free, so it pairs well with the training-time levers above.

Code

Three ways to weight or resample for imbalance·python
# 1. class_weight in model.fit()
model.fit(x, y, class_weight={0: 1.0, 1: 10.0})

# 2. sample_weight array (per-sample importance)
weights = np.where(y_train == 1, 10.0, 1.0)
model.fit(x, y, sample_weight=weights)

# 3. Oversample minority class in the pipeline
# (duplicate minority samples or use SMOTE)

External links

Exercise

Take MNIST and artificially imbalance it (90% one class, 10% rest). Train without weighting and with class_weight. Compare precision and recall on the minority class — note how class_weight shifts the trade-off.

Progress

Progress is local-only — sign in to sync across devices.
Spotted a bug or have feedback on this page?Report an Issue

Comments 0

🔔 Reply notifications (sign in)
Sign inPlease sign in to comment.

No comments yet — be the first.