Lensa ML

Class Imbalance & Resampling

Step 1 of 6: What is Class Imbalance?

When one class dominates

Minority class %10%
ratio9.0:1majorityminority
Majority
90
Minority
10
Ratio
9.0:1

Moderately imbalanced: the minority class is under-represented, and plain training will favor the majority.

Drag the minority class slider from 1% to 50% and watch the ratio in the donut. Imbalance affects training as well as evaluation. With a 99:1 split, the cross-entropy loss is dominated by majority examples, so gradient descent can cut the loss sharply by pushing every prediction toward the majority. Real datasets are often more skewed than this slider allows: fraud, ad clicks, and rare diseases commonly sit below 1% positives. A model trained on rebalanced data also overestimates minority probabilities when deployed on the real base rate.

Step 2 of 6: The Accuracy Paradox

99% accurate, 0% useful

Minority class %5%
"Predict all majority" classifierPred: majorityPred: minorityActual majorityActual minorityTN190FP0FN10TP0Accuracy: 95.0%But recall on minority = 0% — it misses every case!
Accuracy
95.0%
Precision
N/A
Recall
0%
F1 score
N/A

A model that always predicts the majority class gets 95.0% accuracy but zero recall on the minority: it never catches the fraud, disease, or rare event. Accuracy alone misleads when classes are imbalanced.

Drag the minority slider from 1% to 20% and watch accuracy fall from 99% to 80% while recall stays at 0%. Precision and F1 show N/A because the model never predicts the minority class, so precision is 0/0. Compare every real model with this do-nothing baseline before trusting its accuracy. Metrics that correct for chance agreement expose it at once. Cohen's kappa scores this baseline exactly 0, and the Matthews correlation coefficient (MCC) is undefined and conventionally reported as 0.

Step 3 of 6: Random Oversampling

Duplicate the minority

Oversampling ratio1x
majority (n=50)minority originalexact copy (1 ring each)
Original min
10
After oversample
10
Dataset size
60
New ratio
5.0:1

No oversampling yet: the minority class is still under-represented.

Drag the oversampling ratio from 1x to 5x. The 10 minority points become up to 50, matching the 50 majority points at 5x. Oversampling must happen after the train/validation split and only on the training part. If duplicates are made first, copies of one point can land in both sets, and the validation score rewards memorization. Random undersampling is the mirror option: it drops majority points instead, which is fast on large datasets but throws information away.

Step 4 of 6: SMOTE

Synthetic minority samples

Synthetic points0
original minoritysynthetic (SMOTE)majority
Original
8
Synthetic
0
Total minority
8
Avg pair dist
—

No synthetic points yet. Drag the slider to generate SMOTE samples between nearby minority pairs.

Drag the synthetic points slider from 0 to 30. SMOTE (Chawla et al., 2002) picks a minority point, picks one of its k nearest minority neighbors (k = 5 by default), and places a new point a random fraction of the way between them. This demo does the same, with k = 5. SMOTE assumes the space between two minority points is also minority territory, which fails when a majority cluster sits between them. Variants such as Borderline-SMOTE and ADASYN generate points near the class boundary instead.

Step 5 of 6: Class Weights

Penalize majority errors less

Minority class weight1x
boundary← predict majoritypredict minority →
Weight
1x
Boundary
215
Precision
89%
Recall
67%

Equal weights. The boundary sits where the fewest training points are misclassified, and because the classes overlap it misses 4 of the 12 minority points. Raise the weight to make those misses cost more.

Drag the minority class weight from 1x to 10x. A weight w multiplies each minority example's loss, so one misclassified minority point costs as much as w majority points. The two classes overlap here, so at 1x the boundary misses 4 of the 12 minority points, a recall of 67%. By 6x recall reaches 100%, while precision falls from 89% to 40%. The usual default is inverse frequency, n_samples / (n_classes × n_c). For these 45 majority and 12 minority points that gives about 2.4 for the minority class and 0.63 for the majority. Weighting needs no new data and is available in most libraries through a class_weight setting, including logistic regression, SVMs, tree ensembles, and neural network losses.

Step 6 of 6: Better Metrics

Beyond accuracy

Classification threshold0.50
RecallPrecision0.250.250.500.500.750.751.001.00Precision–recall curve (illustrative shape)
Threshold
0.50
Precision
70%
Recall
51%
F1 score
59%

Near-optimal threshold: the best F1 of 59% balances precision and recall.

Drag the classification threshold from 0 to 1 and follow the dot along the precision-recall curve. At threshold 0 every example is flagged, so precision falls to the positive rate, 15% on this curve, which is also what a random classifier scores. F1 peaks at about 59% near threshold 0.43. Moving the threshold is often the cheapest fix for imbalance: train on the original data, then pick the threshold on validation data to reach the precision or recall the application needs.

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

References

  1. Chawla, Bowyer, Hall & Kegelmeyer (2002), "SMOTE: Synthetic Minority Over-sampling Technique", JAIR
  2. He & Garcia (2009), "Learning from Imbalanced Data", IEEE TKDE
  3. Lin, Goyal, Girshick, He & Dollár (2017), "Focal Loss for Dense Object Detection"
  4. 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