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 03 of 09

03Balance the data

The first family of remedies changes the training data until the two classes are about equally common. There are two ways to get there: remove legitimate payments, or add frauds.

3.1Undersampling: throw data away

Random undersampling keeps all 281 frauds and draws 281 of the 144{,}505 legitimate payments at random. The data are now balanced, and the model trains in a blink. But 144{,}224 legitimate payments, 99.8\% of them, are thrown away, and with them most of what the data could teach about normal behavior.

The model now depends heavily on which 281 payments happened to be drawn. Repeat the recipe with a different random draw and you get a different model: over five draws, the number of false positives on the test day at threshold 0.5 ranged from 5{,}386 to 16{,}136 (Figure 2a). A model that changes a lot when the training sample changes has high variance; it is unstable, in the language of the bias–variance tradeoff of Episode 2. Undersampling is still useful when the data are enormous, and averaging many models built on different undersamples reduces the variance again (He and Garcia, 2009). With our data, it is too expensive.

3.2Oversampling: copy the frauds

To keep all the data, go the other way. Random oversampling copies frauds until there are as many frauds as legitimate payments: 144{,}505 frauds, about 514 copies of each. Nothing is thrown away. But nothing is added either: a copy is the same point again. A flexible model, such as a decision tree, can then draw a tiny region around each copied point and memorize it, which is overfitting (Chawla et al., 2002; He and Garcia, 2009). Copies add weight, not information.

3.3SMOTE: create new frauds

Chawla et al. (2002) proposed to create new, synthetic frauds instead: the Synthetic Minority Over-sampling Technique, SMOTE. A new fraud is placed on the line segment between a real fraud and one of its fraud neighbors:

  1. pick a fraud x from the training data;
  2. find its k=5 nearest neighbors among the frauds (Euclidean distance on the standardized features);
  3. pick one of them at random, x_{\mathrm{nb}};
  4. draw a number \lambda uniformly between 0 and 1;
  5. create the new fraud

    x_{\mathrm{new}}=x+\lambda\,(x_{\mathrm{nb}}-x); \tag{3}
  6. repeat until the classes are balanced; here, 144{,}224 new frauds.

At \lambda=0 the new point is x itself, at \lambda=1 it is the neighbor, and in between it is a mixture of the two, feature by feature. Figure 2b shows one step in two of the 29 features (the real algorithm works in all 29). Instead of stacking copies on the same points, SMOTE fills in the region between neighboring frauds, so the region the model assigns to fraud becomes larger and less specific, which is exactly what Chawla and colleagues wanted.

(a) Random undersampling, repeated five times with the same recipe (all 281 frauds and 281 random legitimate payments): the false positives on the test day at threshold 0.5. (b) One SMOTE step in two of the 29 features: a fraud x, its five nearest fraud neighbors (circles, found in these two features for the picture), one chosen neighbor x_{\mathrm{nb}} and the new synthetic fraud for \lambda=0.4. Computed for this tutorial, with the same data as the companion video.
Figure 2. (a) Random undersampling, repeated five times with the same recipe (all 281 frauds and 281 random legitimate payments): the false positives on the test day at threshold 0.5. (b) One SMOTE step in two of the 29 features: a fraud x, its five nearest fraud neighbors (circles, found in these two features for the picture), one chosen neighbor x_{\mathrm{nb}} and the new synthetic fraud for \lambda=0.4. Computed for this tutorial, with the same data as the companion video.
Try itMake synthetic frauds with SMOTE

The real frauds of day 1 (red) and 1,500 legitimate payments (grey) in two of the 29 features, as in Figure 2b. Press the button to run one SMOTE step: pick a fraud x, one of its five nearest fraud neighbors x_{\mathrm{nb}} (circled), and a random \lambda; the new fraud is x+\lambda(x_{\mathrm{nb}}-x), equation (3). Move \lambda to slide it along the segment.

Worked example 2 (SMOTE on day 1). Train the same logistic regression on day 1 plus the 144{,}224 synthetic frauds, and test once on day 2. At threshold 0.5 the model now flags 178 of the 211 frauds: recall rises from 56\% to 84\%.

SMOTE has its own weaknesses. It assumes that the space between two frauds is fraud too. When frauds sit close to legitimate payments, as the fraud x in Figure 2b does, the new points can land among legitimate ones and blur the boundary, a problem He and Garcia (2009) call over-generalization. Distances need standardized numeric features; categorical features need a variant, SMOTE-NC, proposed in the same paper. And the synthetic points are made from the training frauds only, a rule we return to in Chapter 6.

Pause and think. What does SMOTE become if \lambda is always 0?

Show the answer

Random oversampling: by (3) every new fraud is an exact copy of an existing fraud. SMOTE is oversampling with the copies spread out along the segments to the neighbors.

All three methods make the frauds count more by changing the data. But copying a fraud 514 times just makes the loss count it 514 times. Is there a simpler way? Chapter 4 has it.