Contents
08Interview questions and model answers
Q1How do you handle an imbalanced dataset?
Evaluate with precision and recall, the precision–recall curve or the real cost, never accuracy. Then three classic families (He and Garcia, 2009): balance the data (undersample the majority, oversample the minority, or create synthetic minority examples with SMOTE), weight the classes in the loss, and set the decision threshold from the costs of the two errors. After balancing or weighting, correct the probabilities by dividing the odds by the balancing factor. Often the simplest fix, the plain model with a cost-based threshold, is enough: rebalancing is equivalent to moving the threshold (Elkan, 2001).
Q2Why does a model trained on imbalanced data rarely predict the rare class?
The loss is a sum with one term per example, so the common class dominates it; on our day 1, only 281 of 144{,}786 terms are frauds. For logistic regression the effect is exact: the zero derivative with respect to the intercept forces the average predicted probability to equal the fraction of positives, 0.19\%, so only extreme cases reach 0.5.
Q3Compare undersampling and oversampling.
Undersampling throws away majority examples: fast and balanced, but it discards information and gives an unstable, high-variance model: each random draw gives a noticeably different model. Oversampling keeps everything but repeats minority points, which adds no information and lets flexible models overfit them. Both change the class balance and therefore the predicted probabilities.
Q4Explain SMOTE. What are its weaknesses?
For a minority example x, pick one of its k nearest minority neighbors x_{\mathrm{nb}}, draw \lambda uniformly in [0,1], and add x+\lambda(x_{\mathrm{nb}}-x); repeat until balanced (Chawla et al., 2002). It spreads the minority region instead of stacking copies. Weaknesses: it can create points among the majority, it needs scaled numeric features (SMOTE-NC for categorical ones), and it must be applied to the training data only.
Q5Why is a class weight of w the same as copying each example w times?
The loss has one term per example; a copy adds the same term again, so w copies contribute w times the term, exactly what the weight does. Same objective, same model.
Q6After balancing, the predicted probabilities are far too high. Why, and how do you fix them?
The model learned the balanced world: its weighted average prediction is one half. If the frauds were weighted or copied w times, the balanced odds are w times the real odds, so divide the odds by w: \operatorname{logit}p=\operatorname{logit}p_{\mathrm{bal}}-\ln w. A balanced 50\% with w=514 is a real 0.19\%. Divide the odds, not the probability.
Q7How do you choose the classification threshold?
From costs. Flagging costs (1-p)C_{\mathrm{FP}} in expectation and passing costs pC_{\mathrm{FN}}, so flag when p\ge C_{\mathrm{FP}}/(C_{\mathrm{FP}}+C_{\mathrm{FN}}). With a false negative at 120 euros and a false positive at 5, the threshold is 4\%. If the probabilities are not trusted, tune the threshold on validation data, never on the test set.
Q8Where in the pipeline does resampling go?
After the split, inside each training fold only. Resampling before cross-validation puts information about the validation examples into training: in our data, SMOTE before the split inflated the cross-validated average precision from 0.69 to 0.99. Validation and test data stay in their real proportions.