2.1.11 · D1Data Preprocessing & Feature Engineering

Foundations — Handling imbalanced datasets (SMOTE, undersampling)

2,369 words11 min readBack to topic

This page assumes you have never seen a single symbol from the parent note. We build each one from a picture before it is ever used. Read top to bottom; nothing appears before it is earned.


0. What is a "dataset" and a "class"?

Before any math, fix the picture. Imagine a table. Each row is one thing you observed — one transaction, one medical scan, one email. Each column is a measured number about that thing — the amount of money, the tumor size, the number of links in the email.

Figure — Handling imbalanced datasets (SMOTE, undersampling)

Look at the figure: the black rows are samples, the black columns are features, and the single red column on the right is the label. That red column is the whole reason this topic exists — everything is about predicting it.


1. The symbol — a bold letter for a whole row

The parent note writes things like and . What is that?

Why bold? Plain means a single number. Bold warns you: "I am not one number, I am a whole list of numbers travelling together." The parent note needs this because the oversampling technique on that page (which we will meet later and whose name spells out to Synthetic Minority Oversampling Technique) moves entire rows around in space, not one number at a time.

The little subscript is just a name-tag:

  • = "the -th sample" (sample number ).
  • = "some other sample, number ".
  • = "the sample sitting nearby".
  • = "the brand-new synthetic sample we invent".

The double subscript means "sample , feature " — pick a row ( from 1 up), then pick a column ( from 1 up to ), and you get back one plain (non-bold) number.


2. Counting the classes — and

Now we count rows by label — that is, by the value of .

Figure — Handling imbalanced datasets (SMOTE, undersampling)

In the figure, the tall black bar is and the tiny red bar is . That gap between bar heights is the imbalance. Every technique tries to make those two bars the same height.

Why a ratio and not a difference? Because a difference of "9800 rows" means nothing on its own — 9800 out of ten billion is balanced, but 9800 out of 9900 is disaster. Dividing cancels the overall size and leaves the pure skew. This is exactly the reasoning behind why raw accuracy misleads (see 3.2.4-Precision-recall-and-F1-score).


3. The target ratio — the dial you turn

The parent uses in .

Why the prime mark? (read "N-maj-prime") means "the majority count after we edited it". The prime is a bookkeeping tick: unprimed = before, primed = after. So reads: "after editing, keep times the minority count of majority samples."


4. Fractions and percentages — the "Loss" symbol

The parent's undersampling "Loss" is:

Read it piece by piece.

Why subtract from 1? Because "kept" and "lost" together must make the whole (100%). One is minus the other. This tiny move — flip a keep fraction into a loss fraction — is worth memorising; it appears everywhere.


5. Absolute value and distance — the symbol

The oversampling technique needs to know which dots are "nearby". That needs a ruler. First, one tiny tool.

Figure — Handling imbalanced datasets (SMOTE, undersampling)

Look at the figure. The red line is the direct distance . The two black legs are the differences in each feature. Squaring makes every difference positive (no cancelling), adding combines them, and the square root undoes the squaring to give back a real length in the same units as the axes.

So the general formula the parent writes, reads in words: "for every feature, take the gap, square it, add all the squares, then square-root the total."


6. Nearest neighbours and the letter

Why "same class"? The oversampling technique only wants neighbours that are also minority (), so the invented point stays inside minority territory and never drifts into majority land.


7. Making a point "between" — and interpolation

The oversampling technique's heart is:

Figure — Handling imbalanced datasets (SMOTE, undersampling)

Read the figure with the formula:

  • is the arrow from the start dot to the neighbour dot (the black arrow).
  • shrinks that arrow: gives no movement, gives the whole arrow.
  • Adding it to slides you from the start dot part-way toward the neighbour.

So lands the red synthetic dot 40% of the way along the segment. It is guaranteed to sit between two real minority dots — a plausible new example, never a wild outlier.


The name SMOTE

Now that every ingredient exists, we can safely read the parent's headline acronym:


Prerequisite map

Samples and features

Feature vector x

Label y i

Class counts N maj and N min

Imbalance ratio and target r

Loss fraction and percent

Euclidean distance d

k nearest neighbours

Interpolation with lambda

Undersampling

SMOTE

Handling imbalanced datasets

Every foundation above feeds one of the two parent techniques, which feed the topic. Undersampling needs only counting and fractions; SMOTE additionally needs distance, neighbours, and interpolation.


  • 2.1.3-Feature-scaling-and-normalization — run before distance-based SMOTE.
  • 3.2.4-Precision-recall-and-F1-score — the metrics that expose imbalance.
  • 3.4.7-Cost-sensitive-learning — an alternative fix that reweights instead of resampling.
  • 2.1.8-Cross-validation-and-holdout-methods — how to test fairly after resampling.
  • 4.1.2-Decision-trees-and-random-forests — models often paired with these fixes.
  • 2.1.10-Dealing-with-missing-data — a sibling preprocessing step.

Equipment checklist

What is one sample in a dataset?
One row — a single observed thing described by its feature values.
What does the symbol stand for?
The label (class) of sample — one value per row, e.g. for legit and for fraud.
What does bold mean versus plain ?
Bold is a whole list of numbers (a vector, one row); plain is a single number.
What does the symbol count?
The number of features — how many entries each vector has.
What do and count?
The number of samples in the majority (common) and minority (rare) classes.
What is the imbalance ratio and why divide?
; dividing cancels total size so only the skew remains.
What does the prime in signal?
The majority count after editing (before = unprimed, after = primed).
Convert the fraction to a percent.
— multiply by 100 and add the sign.
What does (absolute value) do?
Gives the size of ignoring its sign, so — used so a gap never comes out negative.
What does tell you to do?
Add one term for every feature from 1 to — loop and total.
Why square-root the sum of squared gaps?
It gives the true straight-line (Euclidean) diagonal, not the longer city-block path.
Why must features be scaled before SMOTE?
Otherwise a large-ranged feature dominates the distance and biases the neighbours.
In , what does control?
How far along the line between the two dots the new synthetic point sits (0 = at , 1 = at the neighbour).
What does the acronym SMOTE expand to?
Synthetic Minority Oversampling Technique.