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
Imbalanced Data/Chapter 06 of 09

06Interview follow-ups

6.1Do you even need to rebalance?

Often not. The last row of Table 1 is the plain model, trained on the raw data, with the threshold lowered to 4\%: it already reaches 74\% recall with 296 false positives. Its probabilities are already in the real proportions, by (2), so Proposition 3 applies to it directly. Figure 5a shows recall against false positives at every threshold for the plain model and the corrected weighted model: the two curves are close, and each remedy is mostly a different point on a similar curve. Since rebalancing is equivalent to moving the threshold (Elkan, 2001), the first thing to try is the threshold. Rebalancing or weighting earns its place when it changes the model itself for the better, for example a flexible learner that otherwise never splits on the rare class, or a model that needs many more examples of the rare class to learn its shape.

(a) Recall against false positives on the test day as the threshold moves, for the plain model and the weighted model after the correction; dots: the operating points of Table 1. The uncorrected weighted model at 0.5 lies on the orange curve, because the correction keeps the order of the scores. (b) Average precision of SMOTE followed by logistic regression: estimated by five-fold cross-validation on day 1 with SMOTE applied before the split (red) and only inside the training folds (blue), and measured on the test day. Computed for this tutorial, with the same data and model as the companion video.
Figure 5. (a) Recall against false positives on the test day as the threshold moves, for the plain model and the weighted model after the correction; dots: the operating points of Table 1. The uncorrected weighted model at 0.5 lies on the orange curve, because the correction keeps the order of the scores. (b) Average precision of SMOTE followed by logistic regression: estimated by five-fold cross-validation on day 1 with SMOTE applied before the split (red) and only inside the training folds (blue), and measured on the test day. Computed for this tutorial, with the same data and model as the companion video.

6.2Resample before or after the split?

After, and only inside the training data. Suppose we run SMOTE on all of day 1 and then cross-validate (Episode 11). A synthetic fraud is built from a fraud and its neighbor; when that fraud or its neighbor falls in the validation fold, the model has been trained on a near copy of a validation point. The cross-validated average precision comes out at 0.99. Done right, with SMOTE applied only to the training folds and the validation fold left untouched, it is 0.69, and the test day gives 0.71, which confirms the second number (Figure 5b). The rule covers every step that looks at the data: undersampling, oversampling, SMOTE, scaling, feature selection. Put them in a pipeline that is fitted inside each training fold, and keep the validation and test data in their real proportions.

6.3Which metric do you report?

Not accuracy: never flagging anything already scores 99.85\% on our test day. Report the precision–recall curve and its average precision, which focus on the rare class (Episode 12), or better, the real cost at the chosen threshold: here C_{\mathrm{FP}}\cdot\mathrm{FP}+C_{\mathrm{FN}}\cdot\mathrm{FN}. And if the probabilities themselves are used, check their calibration (log loss, Episode 8) on data in the real proportions, after the correction.

Pause and think. A teammate’s notebook applies SMOTE, then splits into training and test sets, and reports average precision 0.99. What is wrong, and what do you expect on new data?

Show the answer

The test set contains synthetic frauds interpolated from training frauds: near copies of training points. Expect much less on real new data; in our case about 0.7.