Sub topic 8
SMOTE: generating synthetic minority samples
This chapter assumes you know how to split data with scikit-learn and have met random oversampling. You also need the imbalanced-learn package (pip install imbalanced-learn).
The problem with copying rows
Random oversampling duplicates existing minority rows. A flexible model can memorize those exact points, so it overfits the minority class. SMOTE (Synthetic Minority Over-sampling Technique) avoids exact copies. It creates new minority points that lie between existing ones.
How SMOTE works
For each synthetic sample, SMOTE does the following:
- Pick a random minority sample
x. - Find its
knearest minority neighbours. The default isk_neighbors=5. - Pick one neighbour
x_nat random. - Draw a number
λuniformly between 0 and 1. - Create the new point
x_new = x + λ * (x_n - x).
The new point lies on the line segment between two real minority points.
Running SMOTE
The example below splits first, then resamples only the training data.
from collections import Counter
from imblearn.over_sampling import SMOTE
from sklearn.datasets import make_classification
from sklearn.model_selection import train_test_split
X, y = make_classification(
n_samples=10000,
n_features=20,
n_informative=5,
weights=[0.95, 0.05],
flip_y=0.01,
random_state=42,
)
X_train, X_test, y_train, y_test = train_test_split(
X, y, test_size=0.25, stratify=y, random_state=42
)
print("before:", Counter(y_train))
smote = SMOTE(k_neighbors=5, sampling_strategy=0.5, random_state=42)
X_res, y_res = smote.fit_resample(X_train, y_train)
print("after: ", Counter(y_res))
print("shapes:", X_train.shape, "->", X_res.shape)
print("test set untouched:", Counter(y_test))Before resampling, class 1 is about 5% of the training rows. With sampling_strategy=0.5, the minority count becomes half the majority count. The majority class is unchanged, and only the minority class grows. The default, "auto", balances the classes fully, which is often more than you need.
Common errors
- Resampling before splitting. Synthetic points are built from real neighbours. If you resample first, copies and near-copies of a sample can land in both train and test, so the score is inflated. Split first, and in cross-validation resample inside each training fold. The pipeline chapter covers this.
- Unscaled features. SMOTE uses nearest-neighbour distances. A feature with a large range dominates the neighbour search. Scale features before SMOTE, inside the same pipeline.
- Categorical columns. Interpolating between category codes gives meaningless values such as 1.37. Use
SMOTENCfor mixed data orSMOTENfor all-categorical data. - Too few minority samples. SMOTE needs more minority samples than
k_neighbors. Lowerk_neighborsif the minority class is tiny. - Noise and class overlap. If a minority point sits inside majority territory, SMOTE interpolates from it and spreads the noise.
BorderlineSMOTEandADASYNfocus on harder regions, but they can amplify noise too.
When SMOTE helps
SMOTE works best with moderate dimensionality and numeric features. The minority class should form reasonably compact regions. It is less reliable in very high dimensions, where "nearest" loses meaning. It also does little for models that already handle weights well. Synthetic samples change the training distribution, so predicted probabilities become biased toward the minority class. Check calibration afterward, which a later chapter covers.
Exercise
A teammate runs SMOTE on the full dataset, then splits into train and test and reports a high minority recall. Why is the number unreliable, and what is the fix?
Show answer
Synthetic points are interpolated from real minority samples, so the test set then contains points that sit between, or very near, training samples. The model has effectively seen the test data, and the recall is inflated. Split first with stratification, fit SMOTE on the training data only (inside a pipeline during cross-validation), and evaluate on the untouched test set.