Machine learning with football · Chapter 7 · Datasets and overfitting

Imbalanced datasets

When one class is rare, most batches never see it. Downsample, then upweight.

Estimated time: 10 minutes · page 4 of 12

When one class is rare

Home wins are close to half of all matches, so chapter 4's dataset was class-balanced. Many interesting questions aren't. Choose the rare class on the board, matches with at least a set number of goals. At 5 goals it's 16% of matches in Premier League, at 6 even fewer. The common class is the majority class, the rare one the minority class.

The trouble is the batches. With a batch of 20 matches and a rare class of 5%, a typical batch holds one example or none, so most gradient steps never see what you're trying to learn. Accuracy is misleading too, as chapter 4 showed.

Downsample, then upweight

  1. Downsample the majority class: keep only one example in, say, three. The minority class now turns up in most batches.
  2. Upweight the kept majority examples by the same factor, so each counts three times in the loss. Without this the model thinks the rare class is three times as common as it is, which is a prediction bias.

Pick a factor on the board and watch the minority's share of the training data rise. The factor is a hyperparameter, so you try a few. The second Python script shows the bias appear after downsampling and disappear after upweighting.

Check yourself

Only 1 match in 50 ends with a red card for the home side. With batches of 20 matches, what goes wrong?
You downsample the majority class by 3. What must you do so the predictions are not biased?

Leagues