Machine Learning ยท medium

How do you handle class imbalance in classification?

Asked in ML Engineer interviews, in the Machine Learning round.

Short answer

Resampling (SMOTE/undersample), class weights, threshold tuning, PR-AUC over accuracy.

How to answer it

Definition

When one class is rare, accuracy stops being informative and most models default to predicting the majority. The fix is a combination of the right metric, the right threshold, and sometimes a change to the data or the loss.

Start with the metric

1% fraud rate. A model that predicts "not fraud" for everyone:
  accuracy   99%      useless
  recall      0%      catches nothing
  PR-AUC   ~0.01      correctly terrible

Getting the metric right comes first, because every later decision is judged by it. For a rare positive class use precision, recall, F-beta and PR-AUC.

The levers, roughly in order

1. Threshold tuning      free. The model already outputs a probability; the
                         0.5 cutoff is a default, not a result
2. Class weights         class_weight="balanced", or scale_pos_weight in
                         XGBoost. Reweights the loss, invents no data
3. Undersample majority  faster training, discards real data
4. Oversample / SMOTE    synthesises minority points. Fit INSIDE the CV fold
5. Different framing      anomaly detection when positives are very rare or
                         not well represented by past examples

Example

from sklearn.linear_model import LogisticRegression
from sklearn.metrics import precision_recall_curve

model = LogisticRegression(class_weight="balanced").fit(X_train, y_train)
probs = model.predict_proba(X_val)[:, 1]

prec, rec, thresh = precision_recall_curve(y_val, probs)
# pick the threshold where recall clears the business requirement,
# then read off what precision that costs

Key points

FAQ

The 30-second answer

The first thing I would change is the metric, because accuracy is meaningless when one class is one percent: predicting the majority every time scores ninety-nine percent. I would use precision, recall and PR-AUC instead.
Then the cheapest lever, which is the decision threshold. The model outputs a probability and zero point five is just a default, so I would pick the threshold off the precision-recall curve at whatever recall the business needs.
After that, class weights to reweight the loss without inventing data, and only then resampling. If I use SMOTE it has to be fitted inside the cross-validation fold, because resampling before the split leaks synthetic neighbours into validation. And I would never resample the test set, since it has to reflect the real balance.

In the interview

Practice this for real