Home / Machine Learning

Training on Imbalanced Data

By Updated

Select a mitigation strategy to see how model parameters adapt to catch rare minority samples. Compare the standard biased boundary with the optimized one.

95%
Insight

SEVERE IMBALANCE

The standard model (yellow) will likely fail. Watch the optimized model (green) try to compensate.
Boundary Comparison
Standard
Optimized

Standard Model

Vanilla Log Loss
Accuracy 0%
Precision 0%
Recall 0%

Optimized Model

Weights
Accuracy 0%
Precision 0%
Recall 0%

Evaluating optimization...

Overview

When positives are rare, a classifier can achieve high accuracy by mostly predicting the majority class while missing the cases that matter. With 1% positives, always predicting negative gives 99% accuracy and zero recall, so evaluate class-specific errors as well as the overall score.

What imbalance changes

Imbalance alone does not prove a model is wrong. First measure performance in a way that reflects the task: inspect the confusion matrix, precision, recall, F1, and a precision-recall curve. Compare results with the prevalence baseline and decide what false positives and false negatives cost.

Three different interventions

  • Change the decision threshold: keeps the fitted model fixed and trades precision for recall. Tune on validation data and inspect the resulting workload.
  • Change loss weights: class weights make errors on a rare class count more during fitting. This can shift the learned boundary but does not create new examples.
  • Change the training sample: oversampling adds or synthesizes minority examples; undersampling removes majority examples. This changes the data seen during fitting and can increase overfitting or discard useful variation.

Try threshold adjustment and sensible evaluation first. If the model still fails the task, compare weights or resampling using the same validation procedure.

Read the visualizer

Set Class 0 Ratio to 50, then press Retrain Both Nets. Increase it to 95 and retrain again. The two classes become less evenly represented, and the learned decision region can shift. The demonstration isolates class balance in a small synthetic problem; real performance also depends on feature overlap, sample size, model choice, and random variation.

The visualizer shows why accuracy can remain high while minority-class recall falls. It does not show that changing the ratio alone is always the right fix. Compare class-specific errors and choose a strategy against a defined validation objective.

Compare weighting and resampling

MethodWhat changesTrade-off
Class weightingContribution of each class to training lossCan improve minority recall; may increase false positives or harm probability calibration
UndersamplingSome majority examples are removedCheaper training; discarded examples may contain useful subgroups
Random oversamplingMinority rows are repeatedSimple; exact duplicates can encourage memorization
SMOTESynthetic minority rows are interpolated between neighboursCan help numeric features; may create unrealistic points in sparse, categorical, or high-dimensional data

With 99,500 negative and 500 positive examples, balanced class weights assign a positive example about 199 times the negative example’s weight. This changes the loss contribution; it does not make the observed sample representative of every deployment setting. Check probability calibration before using weighted-model scores as probabilities.

Prevent a misleading evaluation

Split before resampling. Apply oversampling or undersampling only inside each training fold; otherwise near-duplicates or synthetic relatives can cross into validation or test data. Use a pipeline or an imbalanced-learn pipeline to keep sampling within the fold. Preserve the real class distribution in validation and test sets unless the deployment distribution is intentionally different.

ROC AUC measures ranking across thresholds and can be useful, but it does not tell how many alerts will be correct at the operating threshold. Precision-recall results expose that trade-off more directly when positives are scarce. Report the positive prevalence and a confusion matrix at the chosen threshold.

Sources and next steps

Predict, then reveal

About to run: set Class 0 Ratio to its minimum (50). Before it does — what happens to the readout?

Committing to an answer first is the point — the reveal runs the experiment on the visualisation above and reads the real value back, so nothing here is scripted.

Recall check

0 of 3

Say the answer out loud before you reveal it — recalling it is what makes it stick, and rereading it is not.

  1. What does this module say about “What imbalance changes”?

  2. What does this module say about “Three different interventions”?

  3. What does this module say about “Read the visualizer”?

Cheat sheet

Training on Imbalanced Dataset

Try resampling, class weights and threshold tuning on an imbalanced dataset and watch minority-class recall respond.

MACHINE LEARNING · vizlearn.in/machine_learning/training_on_label_imbalanced_dataset.html

Further reading

About the author

Ashish Jangra builds and maintains VizLearn. Every module here is written and the visualisation behind it hand-built, so the numbers in a readout come from the same code that draws the picture. Corrections are genuinely welcome and get priority over everything else — if a page states something wrong, or an animation misrepresents what the algorithm does, get in touch.