Class imbalance is the situation in classification where classes are represented very unevenly – typically in fraud detection, fault detection, rare diseases or ad clicks, where the minority class may amount to a fraction of a per cent. The basic difficulty is twofold. Accuracy stops being informative, because a trivial model always predicting the majority class achieves a splendid-looking result while having no value at all; evaluation must therefore use precision and recall or the area under the precision-recall curve. At the same time, optimising the loss function itself neglects the minority class, because its contribution to total error is negligible. Solutions fall into three groups. At the data level, undersampling the majority class helps, as does oversampling the minority one or synthetic generation via SMOTE, which interpolates between nearby minority points. At the algorithm level, class weighting in the loss or focal loss suppressing easy cases is used. At the decision level, the classification threshold is shifted according to the real costs of both error types, which tends to be the most effective and cheapest measure of all.
If your job were to forecast earthquakes in London and you answered “no” every day, your success rate over the past century would exceed 99.99 per cent – and you would be utterly useless. That is the whole problem in a nutshell. A model learning from such data reaches the same conclusion as a lazy clerk: keeping quiet pays. So you have to change its incentives – fine it a thousand times more for a missed earthquake than for a false alarm – or deliberately pad your textbook with the rare cases so they appear more frequent than they really are.