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
- Downsample the majority class: keep only one example in, say, three. The minority class now turns up in most batches.
- 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.