MoonSay /Tutorials/Machine Learning Interview #13
Contents
  1. 01Introduction
  2. 02What goes wrong
  3. 03Balance the data
  4. 04Class weights
  5. 05Fix the scores
  6. 06Interview follow-ups
  7. 07Common misconceptions
  8. 08Interview questions and model answers
  9. 09Summary
  10. References
Machine Learning Interview #13 · Tutorial

Imbalanced DataSMOTE, Class Weights and Thresholds

Why a classifier ignores a rare class, and the three classic fixes

A tutorial companion to the video Machine Learning Interview #13: Imbalanced Data: SMOTE, Class Weights and Thresholds PDF

01Introduction

1.1The question

“How do you handle an imbalanced dataset?” The interesting class is often the rare one: fraudulent payments, patients with a disease, defective parts, customers about to leave, intrusions in network traffic. Train a standard classifier on such data and it does something perfectly reasonable from its own point of view: it learns that almost everything belongs to the common class. The question tests whether you understand why that happens, which remedies exist, what each one really changes, and how to turn the model’s scores into decisions.

1.2Prerequisites and notation

Prerequisites. Logistic regression and its log loss, the cross-entropy of Episode 8. Precision, recall and the precision–recall curve of Episode 12, and the train, validation and test discipline of Episode 11.

Notation. Each transaction has features x and a label y\in\{0,1\}, where y=1 is fraud, the positive class. The model outputs p=p(y=1\mid x), its probability that the transaction is fraud, and the odds p/(1-p); the logarithm of the odds is the logit. A threshold \tau (the Greek letter tau) turns the probability into a decision: the model flags the transaction when p\ge\tau. A fraud that is flagged is a true positive (TP), a fraud that is not flagged a false negative (FN), and a legitimate transaction that is flagged a false positive (FP). Recall, \mathrm{TP}/(\mathrm{TP}+\mathrm{FN}), is the share of frauds the model flags. The training set has n_+ frauds and n_- legitimate transactions, and w=n_-/n_+ is the number of legitimate transactions per fraud. C_{\mathrm{FN}} and C_{\mathrm{FP}} are the costs of a false negative and a false positive.

1.3A short history

The key fact behind every remedy in this tutorial is old. Prentice and Pyke (1979) showed that a logistic model fitted to a case-control study, where the rare cases are deliberately over-represented, estimates the same slopes as on the whole population; only the intercept moves. Elkan (2001) laid the foundations of cost-sensitive learning: the best threshold follows from the costs of the two errors, and changing the class balance of the training data is equivalent to moving the threshold. King and Zeng (2001) worked out how to correct logistic regression for rare events, and Saerens et al. (2002) how to adjust any classifier’s probabilities to new class proportions. Chawla et al. (2002) proposed SMOTE, the most widely used way of creating synthetic minority examples, and He and Garcia (2009) wrote the standard survey, which sorts the remedies into the three families followed here: the data level (resampling), the algorithm level (cost-sensitive learning, class weights) and the decision level (moving the threshold). Textbook treatments are Murphy (2022, §5.1.2.2, §10.3.8.2) and Bishop (2006, §1.5). All numbers in this tutorial were computed for this tutorial, with the same data and model as the companion video.