Select a mitigation strategy to see how model parameters adapt to catch rare minority samples. Compare the standard biased boundary with the optimized one.
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.
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.
Try threshold adjustment and sensible evaluation first. If the model still fails the task, compare weights or resampling using the same validation procedure.
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.
| Method | What changes | Trade-off |
|---|---|---|
| Class weighting | Contribution of each class to training loss | Can improve minority recall; may increase false positives or harm probability calibration |
| Undersampling | Some majority examples are removed | Cheaper training; discarded examples may contain useful subgroups |
| Random oversampling | Minority rows are repeated | Simple; exact duplicates can encourage memorization |
| SMOTE | Synthetic minority rows are interpolated between neighbours | Can 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.
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.
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.
Say the answer out loud before you reveal it — recalling it is what makes it stick, and rereading it is not.
What does this module say about “What imbalance changes”?
What does this module say about “Three different interventions”?
What does this module say about “Read the visualizer”?
Try resampling, class weights and threshold tuning on an imbalanced dataset and watch minority-class recall respond.