Imbalanced Data: SMOTE, ADASYN, Undersampling and Class Weights
In many real problems the interesting class is rare: faults, fraud, anomalies, defects. A model trained naively may almost never predict it. Common remedies change the training data (random oversampling, undersampling, synthetic minority examples with SMOTE or ADASYN) or the loss (class weights). This page explains each technique, what it actually changes in the model, and its side effects on calibration and on the information kept, and it compares them with the simplest alternative: keep the data and move the decision threshold. In the exercises you implement every technique from scratch; then you measure, on data with 2% positives, whether rebalancing improves ranking, F1 or calibration compared with a plain model whose threshold is tuned.
★☆☆☆☆ Introductory⏱ 5 minGeneral audience. No mathematics required.
When one group is tiny
Imagine learning to spot rare red marbles in a jar where only 2 in 100 are red. If you always guess “blue”, you are right 98% of the time but never find a red one. One fix is to practise on extra copies of the red marbles, or to throw out some blue ones, so you see the reds more often. Another is to give a big prize for finding a red one. A third is simply to say “red” whenever a marble looks even a little bit red.
Rebalancing versus moving the threshold
purple boundary (plain at 0.5): finds 9/12 minority, 2 false alarms · dashed (plain at t): 9/12, 2 false alarms
Rebalancing mostly slides the boundary toward the majority class, which a lower threshold on the plain model also does, without inventing points (SMOTE, rings), duplicating them, or discarding majority data (faded). Rebalancing also inflates the predicted probabilities, so they are no longer calibrated.
Why engineers care
Failures, anomalies and hazards are rare by design. How you handle imbalance decides whether a detector finds anything at all, and whether its probabilities can be trusted in a risk calculation.
📝 Quick check (Level 0)
0/3 answered
1. With 2% positives, always predicting negative gives accuracy…
2. Making copies of the rare examples is called…
3. In the demo, lowering the plain model's threshold…
Level 1: Beginner
★★☆☆☆ Basic⏱ 20 minSecondary-school mathematics: arithmetic and elementary algebra.
The toolbox
Technique
What it does
Side effects
class weights
multiply the loss of minority examples by w
probabilities inflated
random oversampling
duplicate minority examples
same effect as weights in expectation; overfitting risk for flexible models
random undersampling
drop majority examples
loses information; fast
SMOTE
interpolate new minority points between minority neighbours
can create points in majority regions
ADASYN
SMOTE focused on hard minority points near the boundary
amplifies noise
threshold moving
keep the data, lower the decision threshold
none; needs a validation set
better metric
PR-AUC, recall at fixed precision, MCC
—
from sklearn.linear_model import LogisticRegressionLogisticRegression(class_weight="balanced").fit(X, y)from imblearn.over_sampling import SMOTE, ADASYN # imbalanced-learn packagefrom imblearn.pipeline import make_pipeline # resample only inside training foldspipe = make_pipeline(SMOTE(k_neighbors=5), LogisticRegression())
📝 Quick check (Level 1)
0/3 answered
1. SMOTE creates new examples by…
2. class_weight='balanced' sets weights…
3. Resampling must be done…
Level 2: Intermediate
★★★☆☆ Intermediate⏱ 35 minUndergraduate students and practising engineers.
What rebalancing really changes
Weights and duplicates. For a probabilistic model, weighting positives by w multiplies the fitted odds by about w. This is equivalent to shifting the logit by log w, the same as using threshold 1/(1 + w) on the original model. Oversampling by a factor w does the same in expectation.
So rebalancing mostly moves the decision boundary, and in return destroys calibration: a model trained on 50/50 data reports probabilities as if positives were common. The probabilities can be corrected with the prior-shift formula (D0.10).
Synthetic data. SMOTE can also change the boundary’s shape by filling the minority region, which helps some high-variance models but can bridge into majority regions when minority clusters are spread out.
Undersampling throws away majority examples and so raises variance, though it can be combined with ensembles (EasyEnsemble, balanced random forests).
🐍 Exercise 1: rebalancing from scratch
Implement random over- and undersampling, SMOTE (interpolation to minority neighbours), ADASYN (more synthetic points near minority examples surrounded by the majority), 'balanced' class weights, a sample-weighted logistic regression and balanced accuracy; check counts, that synthetic points stay within the minority range, and that weighting raises balanced accuracy.
📝 Quick check (Level 2)
0/3 answered
1. Weighting positives by 10 is roughly equivalent to…
2. After training on 50/50 resampled data, probabilities are…
3. ADASYN puts more synthetic points…
Level 3: Advanced
★★★★☆ Advanced⏱ 30 minGraduate level, with full derivations and proofs.
A practical recipe
Use the right metrics: PR-AUC, recall at a required precision, cost (D0.8, D0.9).
Train a strong model on the original data and choose the threshold on validation data from costs or constraints.
If the learner struggles to pay attention to the minority class (for example few positives per tree split), try class weights first: cheap and equivalent to oversampling.
Try SMOTE-like methods only as a tuned hyperparameter inside cross-validation. Gains are dataset-specific and often vanish with strong learners such as gradient boosting.
If probabilities matter, recalibrate after rebalancing.
Better than any resampling: more minority data (targeted collection, simulation, augmentation that respects physics).
Interview questions.
Does SMOTE help gradient boosting? Rarely. Strong learners plus threshold tuning usually match or beat it (van den Goorbergh et al., 2022; Elor & Averbuch-Elor, 2022).
Why is undersampling sometimes used anyway? It speeds up training on huge majority classes; bagging over many undersamples recovers the lost information.
📝 Quick check (Level 3)
0/3 answered
1. The first thing to try for a 1% positive class:
2. Balanced random forests combine…
3. If calibrated probabilities are needed after class weighting…
Level 4: Expert
★★★★★ Expert⏱ 30 minResearchers: original papers and open problems.
Does rebalancing help?
Exercise 1. With 190 majority and 10 minority points:
balanced class weights are 0.526 and 10.0;
SMOTE’s synthetic points stay inside the minority range;
ADASYN allocates exactly the requested 40 points across the 10 minority examples;
weighting lifts balanced accuracy from 0.95 to 0.99.
Exercise 2: 2% positives. Setup:
logistic regression on quadratic features;
each method is fitted on 70% of 3 000 training cases, and the remaining 30% chooses an F1-optimal threshold;
20 000 test cases, 12 repetitions.
Method
ROC-AUC
PR-AUC
F1 at 0.5
F1 at tuned threshold
ECE
no rebalancing
0.925
0.415
0.299
0.419
0.008
class weights
0.940
0.410
0.211
0.425
0.181
random oversampling
0.940
0.409
0.209
0.426
0.182
random undersampling
0.935
0.366
0.196
0.391
0.193
SMOTE
0.940
0.404
0.218
0.406
0.168
ADASYN
0.939
0.395
0.198
0.403
0.186
Once the threshold is tuned, all methods land within 0.035 F1 of each other. The plain model goes from 0.299 to 0.419 just by lowering its threshold.
At the default 0.5, the rebalanced models are worse than the plain one: they now raise too many alarms.
Rebalancing gave a slightly better ROC-AUC (+0.015, partly faster convergence of the rare-class terms) but no better PR-AUC.
Undersampling lost information: PR-AUC drops by 0.05.
Every rebalancing method destroyed calibration (ECE 0.17–0.19 against 0.008).
The threshold, not the data, was the problem.
🐍 Exercise 2 (research): rebalancing versus threshold tuning
On data with 2% positives, train logistic regression with no rebalancing, class weights, random over- and undersampling, SMOTE and ADASYN; compare ROC-AUC, PR-AUC, F1 at threshold 0.5 and at a validation-tuned threshold, and calibration error on 20 000 test cases.
Imbalanced learning in AI, robotics and aerospace
Fault and anomaly detection: failures are rare; simulation and fault injection add minority data more reliably than SMOTE.
Rare-object detection: focal loss and class-balanced sampling in detectors (E sections).
Conjunction (collision) risk screening: extreme imbalance; threshold choice is driven by risk tolerance.
Predictive maintenance: class weights in gradient boosting, plus calibrated probabilities for maintenance planning.
References
N. V. Chawla, K. W. Bowyer, L. O. Hall, W. P. Kegelmeyer (2002). “SMOTE: synthetic minority over-sampling technique.” JAIR 16.
H. He, Y. Bai, E. A. Garcia, S. Li (2008). “ADASYN: adaptive synthetic sampling approach for imbalanced learning.” IJCNN.
H. He, E. A. Garcia (2009). “Learning from imbalanced data.” IEEE TKDE 21(9).
R. van den Goorbergh, M. van Smeden, D. Timmerman, B. Van Calster (2022). “The harm of class imbalance corrections for risk prediction models.” JAMIA 29(9).
Y. Elor, H. Averbuch-Elor (2022). “To SMOTE, or not to SMOTE?” arXiv:2201.08528.
For a well-specified probabilistic model fitted by maximum likelihood, weighting minority examples by w changes the fitted posterior odds by a factor w; thresholding the weighted model at 0.5 is equivalent to thresholding the unweighted model at 1/(1 + w).
A model trained with minority prior π_s but deployed at prior π can be corrected by multiplying its odds by [π/(1 − π)]/[π_s/(1 − π_s)] (Elkan, 2001; Saerens et al., 2002).
Most of what rebalancing achieves comes from moving the decision threshold, which can be done directly without losing calibration.
Practice
Show:
Level 0
Problem 1Level 0
Method that creates synthetic minority points by interpolation?
SMOTE.
Problem 2Level 0
Removing majority examples is called…?
Undersampling.
Problem 3Level 0
Accuracy of always predicting the majority with 2% positives?
0.98.
Level 1
Problem 4Level 1
Balanced weight of the minority class with 90 negatives and 10 positives?
100/(2·10) = 5.
Problem 5Level 1
Balanced weight of the majority class in that case?
100/180 ≈ 0.556.
Problem 6Level 1
SMOTE point from (0, 0) toward (2, 4) with λ = 0.25?
(0.5, 1).
Problem 7Level 1
Balanced accuracy of always predicting the majority class?
0.5.
Level 2
Problem 8Level 2
Threshold equivalent to weighting positives by 10?
1/11 ≈ 0.091.
Problem 9Level 2
Corrected probability for 0.5 from a balanced-trained model when the true prior is 2%?
0.02.
Problem 10Level 2
Corrected probability for 0.98 at prior 2%?
Odds 49 × 2/98 = 1 → 0.5.
Problem 11Level 2
Duplicates needed to balance 300 majority and 12 minority points?
288.
Problem 12Level 2
Balanced accuracy for y = (0, 1, 1), predictions (0, 1, 0)?
(1 + 0.5)/2 = 0.75.
Problem 13Level 2
Which property of predicted probabilities does rebalancing break?
Calibration.
Level 3
Problem 14Level 3
Explain why class weighting and lowering the threshold have similar effects.
Weighting positives by w makes the maximum-likelihood solution estimate odds w times larger, so the weighted model crosses 0.5 where the original model crosses 1/(1 + w); both move the decision boundary toward the majority class. ∎
Problem 15Level 3
Explain how SMOTE can hurt.
If minority points are scattered or noisy, interpolating between neighbours places synthetic points inside majority regions, teaching the model to call majority areas minority and adding label noise. ∎
Problem 16Level 3
Explain why resampling must happen inside cross-validation folds.
Oversampled or synthetic copies of a minority example in both training and validation folds let the model see near-duplicates of validation points, inflating the estimate; resampling only the training part of each fold avoids this leakage. ∎
Problem 17Level 3
Recommend an approach for a 0.5% failure-prediction problem.
Use PR-AUC and recall at a required false-alarm rate; train a strong model on the original data (optionally with class weights), choose the threshold on validation data from costs, recalibrate probabilities if needed, and seek more failure data (simulation, fault injection) rather than relying on SMOTE. ∎
Level 4
Problem 18Level 4
F1 of the plain model at its tuned threshold in exercise 2?
≈ 0.419.
Problem 19Level 4
ECE of the plain model?
≈ 0.008.
Problem 20Level 4
PR-AUC after random undersampling?
≈ 0.366.
Coding practice
🐍 Code 1 (Level 1): balanced class weights
n / (k n_c).
🐍 Code 2 (Level 1): a SMOTE point
Interpolate.
🐍 Code 3 (Level 1): balanced accuracy
Mean recall.
🐍 Code 4 (Level 2): undo rebalancing
Correct probabilities to the true prior.
🐍 Code 5 (Level 1): undersampling
Equal-sized classes.
Topic test
A timed test drawn at random from this topic's question bank. Each attempt gets new questions. For a longer test across the whole chapter, see the chapter test, or mix topics in the question bag.
Imbalanced Data: SMOTE, ADASYN, Undersampling and Class Weights: topic test
10 questions, drawn at random from a bank of 16. Every attempt is different.
⏱ Time limit: 15 minutes. The test submits itself when time runs out.
Levels covered: 0, 1, 2, 3, 4. No hints or solutions until you submit.