What is class imbalance in machine learning?
Class imbalance is when one class in a classification dataset has far more examples than another, for example 990 normal transactions for every 10 fraudulent ones. A model trained on such data can reach high accuracy by always predicting the common class while missing the rare class it was built to find.
Imbalance is the normal case in many applied problems: fraud detection, rare-disease screening, manufacturing defect detection, churn prediction, and click-through prediction all have minority rates of a few percent or less. The standard responses fall into three groups: resample the training data (oversampling, SMOTE, undersampling), change the loss (class weights, focal loss), and evaluate with metrics that ignore the majority's size, such as precision, recall, and the precision-recall curve.
Key ideas
- Class imbalance means one class vastly outnumbers another in the training data.
- Accuracy rewards always predicting the majority, so it hides failure on the minority class.
- Oversampling and SMOTE add minority examples; undersampling removes majority ones.
- Resampling must be applied only to the training split, after splitting.
- Class weights raise the cost of minority errors without changing the data.
- Precision, recall, F1, and the precision-recall curve measure what matters on imbalanced data.
Short takes on this topic
Common questions
Should I use SMOTE or class weights?
Start with class weights or threshold tuning, because they are simple, need no synthetic data, and work with most models. SMOTE can help weak learners on small tabular datasets, but studies with strong models such as gradient-boosted trees often find little gain over weighting. Compare the options with cross-validation on a metric that suits the task.
How does deep learning handle class imbalance?
The common tools are weighted cross-entropy, oversampling the rare class in each batch, and focal loss. Focal loss, introduced for object detection, multiplies each example's loss by (1 − p_t)^γ, so easy, confidently classified examples contribute little and training concentrates on hard ones. Data augmentation of the minority class is another option for images and audio.
What ratio should I rebalance to?
Full 50/50 balance is not required and often over-corrects. Ratios such as 1:2 to 1:5 are common starting points, and the best one should be chosen like any hyperparameter, on validation data. The test set must keep the real class ratio so that its scores match deployment.
How do I fix predicted probabilities after resampling?
A model trained on rebalanced data learns the training prior, not the real one. Multiply its predicted odds p/(1 − p) by the real class odds divided by the training class odds, then convert back to a probability. Recalibrating on validation data with the true class ratio, for example with Platt scaling or isotonic regression, also works.
Related explainers
- Model Evaluation & Metrics — Precision, recall, F1, ROC curves, and regression metrics
- Data Augmentation — Artificially expand training data with transformed copies to reduce overfitting
- Loss Functions — How networks measure their mistakes
- Cross-Validation — K-fold, stratified, and leave-one-out evaluation
- Classification vs Regression — Two kinds of prediction and when to use each
- Data Preprocessing — Cleaning, scaling, and encoding raw data
References
- Chawla, Bowyer, Hall & Kegelmeyer (2002), "SMOTE: Synthetic Minority Over-sampling Technique", JAIR
- He & Garcia (2009), "Learning from Imbalanced Data", IEEE TKDE
- Lin, Goyal, Girshick, He & Dollár (2017), "Focal Loss for Dense Object Detection"
- Saito & Rehmsmeier (2015), "The Precision-Recall Plot Is More Informative than the ROC Plot When Evaluating Binary Classifiers on Imbalanced Datasets", PLoS ONE
Last updated