Worked examples — Handling imbalanced datasets (SMOTE, undersampling)
This page is the drill hall. The parent note gave you the machinery — undersampling loss, SMOTE interpolation. Here we run that machinery through every kind of input it can meet, so no exam or real dataset surprises you.
Before any numbers, one promise: every symbol here was earned in the parent. If you forgot one, here is the pocket dictionary.
Recall Pocket dictionary (tap to open)
- ::: how many samples the majority (big) class has.
- ::: how many samples the minority (rare) class has.
- imbalance ratio ::: , e.g. 99:1 means 99 big-class samples per 1 rare one.
- ::: the majority:minority ratio you want after undersampling. means perfectly balanced.
- ::: majority samples you keep after undersampling .
- (lambda) ::: a random dial between 0 and 1 that says how far along the line between two minority points a new synthetic point sits.
- ::: one minority data point (a list of feature numbers, like
[2.5, 0.8, 0.6]).
The scenario matrix
Every problem this topic throws is one of these cells. The worked examples below each carry a tag like (Cell C) so you can see the coverage is complete.
| Cell | Scenario class | The trap it hides |
|---|---|---|
| A | Mild imbalance (ratio < 10:1) | Do we even need to resample? |
| B | Extreme imbalance (100:1, 1000:1) | Undersampling loss explodes |
| C | Undersampling with (not full balance) | People confuse "keep" vs "throw away" |
| D | SMOTE single-point interpolation | Getting direction right |
| E | Degenerate: minority has 1 sample | KNN needs neighbours — it breaks |
| F | Limiting case: and | Do we get duplicates? |
| G | Real-world word problem (fraud/medical) | Choosing the right tool |
| H | Exam twist: combine SMOTE + undersample, or resample-then-CV leak | The subtle mistake |
We now walk all eight cells. Where geometry lives (SMOTE, decision regions), a figure carries it.
Cell A — Do we even resample?
Forecast: Guess the ratio and whether it's "dangerous" before reading on.
- Compute the ratio. , i.e. about 2.33 : 1. Why this step? The ratio is the single number that decides urgency. The parent flagged "danger" at > 10:1.
- Compare to the danger line. , so this is mild. A well-tuned model plus precision/recall monitoring usually handles it — no synthetic data needed. Why this step? Resampling isn't free; it distorts the true distribution. You only pay that cost when the imbalance genuinely starves the model.
Verify: Sanity check the fractions add up: ✓. Minority is of data — plenty to learn from.
Cell B — Extreme imbalance and the loss explosion
Forecast: Will you keep more or fewer than 1% of the normal readings?
- Target keep count. . Why this step? means the kept majority must equal the minority count.
- Apply the loss formula from the parent, : Why this step? This quantifies the pain: you discard ~99.9% of normal readings — nearly every learned "normal" pattern vanishes.
- Read the verdict. At 1000:1, pure undersampling is reckless. Prefer SMOTE (Cell D) or cost-sensitive learning.
Verify: Kept thrown all: kept fraction ; loss ; they sum to ✓.
Cell C — Undersampling with a target that is NOT full balance
Forecast: More or fewer than the 100 you'd keep at ?
- Keep count. legit kept. Why this step? deliberately leaves 3 legit per fraud — closer to a realistic prior, which keeps the model from over-crying "fraud".
- Thrown-away count. discarded. Why this step? This is the raw information sacrifice.
- Loss fraction. . Why this step? Even a "gentle" 3:1 still tosses ~97% of majority data at this severe imbalance — the lever barely moves the loss.
Verify: Kept 300 + thrown 9600 = 9900 = ✓. And is bigger than , so we keep 300 > 100 ✓ (more kept when we tolerate more imbalance).
Cell D — SMOTE single-point interpolation (the core move)
Forecast: Will the new size land between 2.5 and 2.7, and where exactly?

- Direction vector. . Why this step? This arrow (yellow in the figure) points from toward its neighbour. Note the third component is negative — irregularity decreases toward the neighbour, which is fine.
- Scale by . . Why this step? means "walk 40% of the way along that arrow". Look at the pink dot on the figure sitting 40% along.
- Add to . . Why this step? The synthetic sample is a plausible malignant point — every coordinate lies between the two real ones.
Verify: Each coordinate must sit between the endpoints. ✓, ✓, and for the decreasing feature ✓. All in-between → valid.
Cell E — Degenerate: the minority has only ONE sample
Forecast: Does it interpolate, duplicate, or crash?
- Count available neighbours. SMOTE searches within the minority class for neighbours. With 1 point, the only "neighbour" is itself — there are 0 other minority points. Why this step? Interpolation needs a second distinct point to draw a line to. There is none.
- What the formula gives. — a pure duplicate for any . Why this step? The direction vector is the zero vector, so no new location can be created. Real libraries instead raise an error ( exceeds available samples).
- The fix. Duplicate the point a few times first (random oversampling) or gather more real minority data; SMOTE cannot invent variety from a single point.
Verify: for every , so ✓ — mathematically no new information.
Cell F — Limiting values of
Forecast: Which one lands on the original, which on the neighbour?
- . — exactly . Why this step? Zero steps along the arrow → you never left home.
- . . Why this step? A full step lands you on the neighbour.
- Consequence. Both endpoints are duplicates of real points, not new information. That is why SMOTE draws from the open-ish interval — the interesting samples live strictly in between.
Verify: At output ✓; at output ✓. Endpoints reproduce originals, confirming the interpolation is a straight line segment.
Cell G — Real-world word problem: pick the tool
Forecast: Would you delete healthy scans or synthesise diseased ones?
- Read the constraint. Minority (50 diseased) is tiny and irreplaceable; majority is plentiful. Why this step? The scarce resource decides the strategy. You never want to lose the precious 50.
- Rule out pure undersampling. Balancing to by cutting healthy to 50 discards scans and, worse, leaves only 100 total — too few to train. Why this step? Undersampling throws away data; here that starves the model (Cell B logic).
- Choose SMOTE (optionally + mild undersample). Synthesise diseased scans up toward the healthy count using scaled features (SMOTE uses Euclidean distance, so scale first!). Synthetic count to reach balance: Why this step? We grow the precious class without deleting anything, and scaling stops the largest-range feature from dominating the distance.
Verify: After SMOTE the diseased class , matching the 1150 healthy → 1:1 balance ✓, and zero real scans lost ✓.
Cell H — Exam twist: the resample-before-split leak
Forecast: Is the high recall real or fake?
- Trace the data flow. SMOTE created synthetic points by interpolating all minority samples — including ones that will later fall into the validation fold. Why this step? A synthetic training point can sit between a real point and a validation point, effectively memorising it.
- Name the leak. This is data leakage: validation information bled into training via synthesis. The recall is inflated and won't hold on truly unseen data. Why this step? CV is supposed to estimate performance on unseen data; the leak breaks that guarantee.
- The fix. Resample inside each fold: split first, then SMOTE only the training portion of each fold (use an imbalanced-learn
Pipeline). Real minority points in the validation fold stay untouched. Why this step? Now no synthetic point ever "sees" validation data — the score is honest.
Verify (conceptual sanity): Leaked-CV recall correct-CV recall almost always, and the gap disappears on a held-out test set the pipeline never touched — the classic symptom of leakage.
Coverage recap
Recall Did we hit every cell?
A mild ratio ::: Cell A (churn 2.33:1, no resample) B extreme loss ::: Cell B (1000:1 → 99.9% loss) C custom ::: Cell C ( keeps 300, loss 96.97%) D SMOTE interp ::: Cell D () E degenerate ::: Cell E (1 minority sample → no neighbours) F limits ::: Cell F ( give duplicates) G word problem ::: Cell G (grow the rare via SMOTE) H exam twist ::: Cell H (resample-before-split leak)
Every scenario the topic can produce now has a worked, verified answer. When a real dataset lands on your desk, find its cell above and reuse the recipe. For the tree-based models that are unusually robust to imbalance, see 4.1.2-Decision-trees-and-random-forests.