Class imbalance

Class imbalance is when one category in a labelled dataset vastly outnumbers another, such as genuine transactions versus fraudulent ones, which can lead a model to overlook the rare class.

Many of the things people most want AI to spot are rare. Google's machine learning glossary describes a class-imbalanced dataset as one in which the number of labels for each class differs significantly. The common label is the majority class and the rare one the minority class. Google's crash course notes that fraudulent transactions can make up less than 0.1% of examples in a fraud dataset.

Imbalance causes two problems. The first is a misleading score. If 999 transactions in 1,000 are genuine, a model that labels everything genuine is 99.9% accurate and catches no fraud at all, which is why accuracy is usually a poor measure here and precision and recall tell you more. The second is that the model barely sees the rare class while training. Google's example uses 200 sunflowers and 2 roses: a training batch of 20 examples will often contain no roses at all.

The standard fixes change what the model sees. Downsampling trains on a fraction of the majority class so that batches contain more minority examples, and upweighting then counts each remaining majority example more heavily by the same factor, so the model still learns how common each class really is. In Google's example, downsampling the sunflowers by a factor of 25 leaves 8 sunflowers for every 2 roses, and each sunflower error is multiplied by 25.

The other route is to add minority examples. The best-known method is SMOTE, the Synthetic Minority Over-sampling Technique, published by Chawla, Bowyer, Hall and Kegelmeyer in the Journal of Artificial Intelligence Research in 2002. Instead of copying rare examples, it creates new synthetic ones along the lines between a minority example and its nearest minority neighbours. The authors found that combining this with undersampling of the majority class beat undersampling alone.

Class imbalance runs through fraud detection, medical diagnosis and fault detection. When a company claims a detector for a rare event is 99% accurate, the useful questions are how rare the event is and what share of real cases the system actually catches.

Common questions

What is class imbalance in machine learning?

It is when the categories a model must learn are very unevenly represented in the training data, such as 1,000 healthy scans for every scan showing a disease. The model sees few examples of the rare class and can learn to ignore it while still scoring well on overall accuracy.

Why is accuracy misleading on imbalanced data?

Because always predicting the common class gives a high score. If only 0.1% of cases are positive, a model that never predicts a positive is 99.9% accurate and useless. Precision, recall and related measures show how well the model handles the rare class.

What is SMOTE?

SMOTE, the Synthetic Minority Over-sampling Technique, is a method published in 2002 for creating new synthetic examples of the rare class. It places each new example between a real minority example and one of its nearest minority neighbours, giving the model more varied rare cases to learn from.

How do you fix class imbalance?

Common approaches are downsampling the majority class and upweighting it to compensate, oversampling or synthesising minority examples, and choosing evaluation measures such as precision and recall. Google's guidance is to treat the amount of downsampling and upweighting like other settings and find the right values by experiment.

Finding more

Search for "class-imbalanced dataset", "downsampling and upweighting" or "SMOTE oversampling" to see the main techniques. Reliable sources include Google's Machine Learning Crash Course, the documentation of established machine learning libraries and the original SMOTE paper. When reading claims about detectors for rare events, look for the base rate and for precision and recall figures rather than accuracy alone.

Working out what this means for your business?

Aileron Shift builds the systems behind this site: AI newsrooms that research, write and publish on their own, marketing automation and WordPress hosting for companies that need to move faster than their competitors.