liveBook · Manning

Class imbalance

As explained in Machine Learning System Design

A condition in which the frequencies of classes in a classification dataset or population differ substantially.

Fraud is usually rare, and its share of transactions can change sharply from day to day. That is class imbalance: one classification class has far fewer examples than another. It matters both when training a model and when deciding whether its reported performance will hold up in production. section 5.2

Check imbalance at three points in the ML workflow:

  • Training: with a heavily overrepresented class, a model trained using cross-entropy loss may learn to output the popular class for every input. Undersampling, oversampling, and class weights are options, but can require substantial tuning. Focal loss instead reduces the contribution of easy, confidently classified examples so training focuses more on hard ones. section 5.1.1
  • Evaluation: ask whether a metric changes when class balance changes. Precision can shift even if a model’s rates of detecting fraud and falsely flagging legitimate transactions stay the same. Recall and specificity are insensitive to that change in balance. Choose metrics for the decision they support, not just for familiarity. section 5.2
  • Data preparation: compare counts at each step that supplies labels. Search results are shown more often than they are clicked, and clicks occur more often than purchases; subsampling may be needed to adjust those balances. section 6.7.2

When purchases are too rare

Suppose 10,000 displayed offers produce about 300 clicks but only 9 purchases. Predicting purchases directly leaves few positive examples and makes A/B tests slow. Click-through rate offers a more frequent proxy for purchase conversion, provided the relationship is checked and the eventual purchase and sales outcomes remain in view. More plentiful clicks make iteration easier; they do not make a click equivalent to a purchase. section 5.2.2

For a search system optimizing clicks and purchases, binary cross-entropy is a starting point, while focal loss is a candidate if those labels are imbalanced. Treat it as a choice to test against the desired outcomes, not an automatic fix for every uneven dataset. section 5.3.2

Figure 5.3 The suggested focal loss function focuses more on misclassified examples while reducing the relative loss for well-classified examples (source: Lin et al.).
figure

The focal-loss figure contrasts the relative loss assigned to misclassified and well-classified examples, clarifying what it changes during training. section 5.1.1

How Machine Learning System Design covers it

How deep it goes
Explains loss and metric tradeoffs through cases, but stops short of a procedure for tuning sampling or focal loss.
Examples
Uses changing fraud rates, rare offer purchases, and unequal search impressions, clicks, and purchases.
What it assumes
Assumes familiarity with binary classification and training losses while working through fraud metrics and explaining focal loss from cross-entropy.
What you can do after
Evaluate/understand it by checking class counts, interpreting metrics against changing class balance, and considering imbalance-aware losses or proxy targets.
How it connects
Links class imbalance to cross-entropy loss, focal loss, precision, recall, specificity, proxy metrics, and A/B testing.

Also explained in

Compare how 11 books explain class imbalance →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Machine Learning System Design. The section links point to the parts of the book it draws on.