Learn · Intermediate
Class imbalance and focal loss: learning when the important examples are rare
Class imbalance occurs when some outcomes are much rarer than others in a dataset. It matters because a model can achieve impressive overall accuracy by ignoring the rare cases that users most need it to find. Choosing a useful model therefore means defining the costs of misses and false alarms, not merely maximizing the number of correct labels.
Imagine a screening system that reviews 10,000 alerts, of which 100 are genuine incidents. A system that labels every alert harmless is 99% accurate, yet it finds none of the incidents. The arithmetic is sound; the choice of measurement is the problem. The right score depends on what happens after a flag and how costly it is to overlook a real case.
Start with the error, not the ratio
For a binary classification task, a true positive is a real incident correctly flagged; a false positive is an ordinary alert flagged as an incident. A false negative is a missed incident. Precision and recall expose different sides of that trade-off: precision is the fraction of flags that are real, while recall is the fraction of real incidents that are flagged. If a tool finds 80 of the 100 incidents but raises 400 flags in total, recall is 80% and precision is 20%. A review team must inspect five alerts, on average, for each genuine incident found.
Those two numbers depend on the decision threshold. A system may output a score and flag anything above 0.5, but that number is a choice, not a law. Lowering the threshold often finds more positives while creating more false alarms. The best operating point depends on review capacity, the cost of a miss, and the cost of an unnecessary intervention. An ROC curve traces one view of threshold trade-offs; when positives are rare, also inspect precision and recall at the thresholds and base rates that matter in deployment.
Why learning can favor the majority
A model is trained to reduce a loss: a penalty measuring its mistakes. When ordinary examples dominate the training set, their combined contribution can dominate the loss too. A model can improve its average score by concentrating on easy, common cases even if performance on the rare class remains poor. The outcome depends on the data, loss, optimizer, and problem; imbalance alone does not guarantee failure.
One response is class weighting: assign a larger training penalty to mistakes on rare examples. Another is resampling: show the model rare cases more often, common cases less often, or both. These are like choosing which exercises a student practices more frequently. They change the emphasis of training; they do not create new information about the rare category. Repeating a handful of unusual examples can encourage memorization, and discarding common examples can throw away useful variety. Always compare against an unmodified baseline on the same untouched evaluation data.
A related method, focal loss, changes how the loss weights examples according to their difficulty. In their paper on focal loss for dense object detection, Tsung-Yi Lin and colleagues address the flood of easy background examples in image detection by down-weighting well-classified examples. The parameter controlling that down-weighting determines how strongly the method concentrates on hard examples. It is a targeted answer to a training problem, not a universal cure: difficult examples can include mislabeled data, and emphasizing them can amplify noise. Ordinary cross-entropy is recovered when the focusing parameter is zero.
Evaluate the system you will actually use
Keep a separate test set that represents the expected deployment population and time period. Split carefully when examples share a person, organization, document, or time window: related examples on both sides can make a model look better than it will on genuinely new cases. The lesson on holdout sets and cross-validation explains why this separation matters. If you rebalance training examples, do it inside each training fold, not before splitting, so duplicated or related records cannot leak into the evaluation fold.
Report the number of actual positive cases as well as percentages. Eighty percent recall on ten incidents means eight detections; a single additional miss moves the score by ten percentage points. A small positive sample also makes differences between models uncertain. Evaluate changes across relevant subgroups and different data periods when enough examples exist, because the overall rate can hide poor performance on a particular kind of case.
Finally, check whether scores are calibrated before treating them as probabilities. Reweighting or oversampling can change the relationship between scores and real-world frequencies, so validate and, if appropriate, recalibrate on representative held-out data. Even a calibrated score is not a decision: deciding whether to investigate still requires a cost and capacity judgment.
Class imbalance is a signal to specify the task more carefully. A practical workflow is to establish a trivial baseline, choose metrics tied to real consequences, compare training interventions, and then test the selected threshold on representative unseen data. Monitor the rate of positives and errors after deployment: if the world changes, yesterday’s precision and review workload can change with it.
Key questions
Why can 99% accuracy hide failure on rare cases?
What does focal loss change during training?
Does balancing the training data make a model’s probabilities reliable?
Cite this
APA
Ground Truth. (2026, November 15). Class imbalance and focal loss: learning when the important examples are rare. Ground Truth. https://groundtruth.day/learn/class-imbalance-and-focal-loss.html
BibTeX
@misc{groundtruth:class-imbalance-and-focal-loss,
title = {Class imbalance and focal loss: learning when the important examples are rare},
author = {{Ground Truth}},
year = {2026},
month = {nov},
url = {https://groundtruth.day/learn/class-imbalance-and-focal-loss.html}
}