← All Posts
Deep Learning · Data Imbalance in Deep Learning · Class Imbalance and Resampling

The Imbalance Problem

A classifier can be 99% accurate while missing every case you care about. Suppose only 10 of 1,000 items are defective. Predicting “not defective” for every item gets the other 990 right, but catches no defects.

Class imbalance means the class counts differ. It becomes a modeling problem when the data, training objective or evaluation hides mistakes that matter. The task is to learn useful distinctions and make useful decisions; equal class counts are not themselves the goal.

Look at the counts before the percentage

Call a defective item positive and an ordinary item negative. These names identify the class we are trying to detect; they do not mean good and bad. In this dataset the positive class is the minority, and its prevalence, or share of observations, is 10 / 1,000 = 1%.

Ten rare cases among a thousand observations
Each square represents 10 items. The blue + square contains the 10 positives; the 99 gray squares contain 990 negatives. A majority-class prediction looks impressive when success is counted across all items equally.

Accuracy counts the fraction of predictions that are correct:

$$\text{accuracy}=\frac{\text{correct predictions}}{\text{all predictions}}.$$

The always-negative classifier gets 990 / 1,000 = 99%. Keep this baseline in the comparison: a large accuracy percentage alone is not evidence of useful detection.

Separate the two kinds of mistakes

A false negative is a missed positive: a defective item called ordinary. A false positive is a false alarm: an ordinary item called defective. Correctly detected positives are true positives; correctly dismissed negatives are true negatives.

Compare the baseline with an illustrative detector that catches 8 of the 10 defects and raises 20 false alarms. These are fixed example outcomes, not a claim about a trained model.

Choose a prediction rule for the same 1,000 items
Actual classPredict positivePredict negative
Positive: 100 detected10 missed
Negative: 9900 false alarms990 correct
ACCURACY99.0%Correct among all items
RECALL0.0%Detected among actual positives
PRECISIONUndefined: no alertsTrue positives among alerts
Always negative: 99% accuracy, but all 10 defects are missed.
The row totals stay fixed. Switching rules changes which mistakes are made, not the underlying class distribution.

Recall asks how many actual positives we found: 8 / 10 = 80%. Precision asks how many alerts were true positives: 8 / (8 + 20) ≈ 28.6%. The detector has lower accuracy, 97.8%, but it performs the detection task the baseline ignores.

That does not automatically make it good enough. Most of its alerts are false alarms. If review is expensive or capacity is limited, that matters. When there are no alerts, precision has a zero denominator and is undefined; libraries may report a chosen fallback value. State that convention rather than interpreting it as measured alert quality.

Decide which mistakes matter for this task

Suppose a missed defect costs 20 evaluation units and a false alarm costs 1. The baseline costs 10 × 20 = 200 units. The detector costs 2 × 20 + 20 × 1 = 60. Under those stated costs, its lower accuracy is an acceptable tradeoff.

If each miss and false alarm instead costs one unit, the baseline makes 10 mistakes and the detector makes 22. The preferred rule changes with the objective. Class counts alone cannot supply those costs.

You might instead require a minimum recall, compare precision at a fixed review budget, or report each class’s recall with balanced accuracy. The evaluation chapter explains how these measurements differ. Choose what success means before choosing a balancing method.

Why the model can struggle, and what imbalance does not prove

With equal per-row loss weights, 990 negative training examples contribute many more terms than 10 positive examples. Reducing mistakes on the majority may therefore dominate the training objective. The default classification threshold can also yield few positive predictions even when the score ranks positives usefully.

But rare does not mean impossible to learn. If a reliable feature cleanly separates the classes, even an imbalanced dataset can be classified well. Conversely, ten positive examples may be too few to represent several positive subtypes, regardless of how often we copy them.

Inspect the minority examples, their labeling quality, overlap with other classes and coverage of the situations expected in use. More representative independent examples can supply information that resampling existing rows cannot. Also, a minority class is not automatically an outlier population: class labels and geometric unusualness answer different questions.

Three changes that act at different places

01Training examplesOversampling repeats or generates rows; undersampling selects fewer rows. Both alter what training sees.
02Training lossClass weights increase or decrease the contribution of selected classes without creating new observations.
03Decision ruleA threshold turns scores into labels. Changing it can trade misses against false alarms without refitting the score model.
Know what you are changing. These choices can be combined, but compare them against a clear baseline. Resampling and reweighting do not guarantee improved ranking or calibrated probabilities.

See random oversampling, random undersampling, class weights and threshold moving for their separate mechanisms. Synthetic methods such as SMOTE add assumptions about what valid new feature vectors look like.

Preserve the population you need to evaluate

Split original observations before resampling. Inside cross-validation, resample only each training fold; keep validation observations independent and representative of the intended workload. Otherwise copies or synthetic neighbors can leak information across the boundary, and a balanced test set can hide the alert volume seen under natural prevalence.

For independent observations, stratified splitting helps preserve class proportions. It does not create more rare events, and group or time constraints still take priority. Always inspect the actual positive count in each evaluation partition.

References and further reading