Handling Imbalanced Datasets using SMOTE
Machine Learning models are only as good as the data they learn from. One of the most common challenges faced by data scientists is imbalanced datasets, where one class significantly outnumbers the other(s). This issue is especially common in fraud detection, medical diagnosis, anomaly detection, manufacturing defect detection, cybersecurity, and loan default prediction.
If not handled properly, imbalanced data can lead to misleading model performance and poor real-world predictions.
In this article, we’ll explore:
-
What is an imbalanced dataset?
-
Why class imbalance is a problem
-
Common techniques to handle imbalance
-
Understanding SMOTE
-
How SMOTE works internally
-
Implementing SMOTE in Python
-
Advantages and disadvantages
-
SMOTE variants
-
Best practices
-
Real-world examples
What is an Imbalanced Dataset?
An imbalanced dataset is one where the number of samples in one class is much larger than the others.
For example:
| Loan Approved | Number of Records |
|---|---|
| Yes | 9,500 |
| No | 500 |
Here,
-
Majority Class = Approved
-
Minority Class = Not Approved
Distribution:
95% Approved
5% Not Approved
Another example:
Fraud Detection
Normal Transactions : 998,000
Fraud Transactions : 2,000
Only 0.2% are fraud cases.
Why is Class Imbalance a Problem?
Suppose we build a classifier.
Dataset:
1000 samples
950 Normal
50 Fraud
Now imagine our model predicts:
Everything is Normal
Accuracy becomes:
950 / 1000 = 95%
95% accuracy sounds excellent.
But…
Fraud Detection Rate:
0%
The model completely fails at identifying fraud.
This demonstrates why accuracy alone is not a reliable metric for imbalanced datasets.
Real-World Applications
Imbalanced datasets appear in many domains.
Healthcare
Healthy Patients : 98%
Cancer Patients : 2%
Credit Card Fraud
Legitimate : 99.8%
Fraud : 0.2%
Manufacturing
Good Products : 99%
Defective : 1%
Network Security
Normal Traffic : 99.5%
Intrusion : 0.5%
Customer Churn
Stayed : 92%
Left : 8%
Problems Caused by Imbalanced Data
1. Biased Model
The algorithm learns mostly from the majority class.
2. Poor Minority Prediction
The minority class is often ignored.
3. Misleading Accuracy
A model can achieve very high accuracy while failing to detect minority cases.
4. Poor Recall
Minority samples are missed.
5. Poor F1 Score
The balance between Precision and Recall deteriorates.
Better Evaluation Metrics
Instead of accuracy, use:
-
Precision
-
Recall
-
F1 Score
-
ROC-AUC
-
PR-AUC (Precision-Recall AUC)
-
Confusion Matrix
Example:
| Metric | Value |
|---|---|
| Accuracy | 97% |
| Precision | 74% |
| Recall | 91% |
| F1 Score | 81% |
Although accuracy is high, Recall and F1 provide a more meaningful evaluation.
Ways to Handle Imbalanced Datasets
There are several approaches.
1. Random Under Sampling
Remove samples from the majority class.
Example:
Before
9000 Yes
1000 No
After
1000 Yes
1000 No
Advantages
-
Faster training
-
Balanced dataset
Disadvantages
-
Loss of valuable information
-
Risk of underfitting
2. Random Over Sampling
Duplicate minority samples.
Before
9000 Yes
1000 No
After
9000 Yes
9000 No
Advantages
-
Simple
-
No data loss
Disadvantages
-
Overfitting
-
Duplicate records
3. Synthetic Data Generation (SMOTE)
Instead of duplicating data,
SMOTE creates new synthetic samples.
This is the most widely used oversampling technique.
What is SMOTE?
SMOTE stands for:
Synthetic Minority Over-sampling Technique
It was introduced to overcome the limitations of random oversampling.
Instead of copying existing minority samples, SMOTE generates entirely new synthetic samples by interpolating between existing minority class samples.
This creates a richer and more representative minority class distribution.
How SMOTE Works
Suppose we have minority class samples:
A = (2,3)
B = (4,5)
SMOTE generates a synthetic point somewhere between them.
For example,
New Sample
(3,4)
This point didn’t exist before.
It is mathematically generated.
Step-by-Step SMOTE Process
Suppose minority samples are:
P1
P2
P3
P4
Step 1
Choose one minority sample.
Example:
P2
Step 2
Find its nearest minority neighbors.
Example:
P1
P3
P4
Step 3
Randomly select one neighbor.
Example:
P3
Step 4
Generate a new point between P2 and P3.
Repeat until the desired number of minority samples is created.
Mathematical Formula
Synthetic sample is generated as:
New Sample
= Sample
+ Random Number ×
(Neighbor − Sample)
where Random Number lies between 0 and 1.
This ensures that synthetic samples lie between real minority instances.
Visual Representation
Original
● ●
●
●
After SMOTE
● ○ ●
○ ●
○
●
Legend:
● Original Minority Sample
○ Synthetic Sample
Installing SMOTE
pip install imbalanced-learn
Import Libraries
from imblearn.over_sampling import SMOTE
from sklearn.model_selection import train_test_split
from sklearn.datasets import make_classification
from collections import Counter
Creating an Imbalanced Dataset
X, y = make_classification(
n_samples=5000,
n_features=10,
n_classes=2,
weights=[0.95, 0.05],
random_state=42
)
print(Counter(y))
Output
Counter({
0: 4750,
1: 250
})
Clearly imbalanced.
Apply SMOTE
smote = SMOTE(random_state=42)
X_resampled, y_resampled = smote.fit_resample(X, y)
print(Counter(y_resampled))
Output
Counter({
0:4750,
1:4750
})
Perfectly balanced.
Train-Test Split (Correct Workflow)
A common mistake is applying SMOTE before splitting the dataset, which causes data leakage because synthetic samples derived from the training data may leak information into the test set.
The correct sequence is:
from sklearn.model_selection import train_test_split
from imblearn.over_sampling import SMOTE
X_train, X_test, y_train, y_test = train_test_split(
X,
y,
test_size=0.2,
stratify=y,
random_state=42
)
smote = SMOTE(random_state=42)
X_train_resampled, y_train_resampled = smote.fit_resample(
X_train,
y_train
)
The test set should remain untouched so it reflects real-world data distribution.
Training a Model
from sklearn.ensemble import RandomForestClassifier
model = RandomForestClassifier(
random_state=42
)
model.fit(
X_train_resampled,
y_train_resampled
)
Evaluate
from sklearn.metrics import classification_report
predictions = model.predict(X_test)
print(classification_report(
y_test,
predictions
))
Notice that Recall and F1 Score for the minority class often improve compared to training without SMOTE.
Comparing Before and After SMOTE
| Metric | Without SMOTE | With SMOTE |
|---|---|---|
| Accuracy | 97% | 95% |
| Precision | 65% | 80% |
| Recall | 38% | 89% |
| F1 Score | 48% | 84% |
Accuracy may decrease slightly, but the model becomes much more effective at identifying minority cases.
Understanding k-Nearest Neighbors in SMOTE
By default:
SMOTE(k_neighbors=5)
This means each minority sample looks at its 5 nearest minority neighbors when generating synthetic examples.
Changing this value affects the diversity and locality of generated samples.
Important SMOTE Parameters
SMOTE(
sampling_strategy='auto',
random_state=42,
k_neighbors=5
)
sampling_strategy
Controls how much oversampling is performed.
SMOTE(
sampling_strategy=0.5
)
The minority class becomes 50% the size of the majority class.
random_state
Ensures reproducible results.
k_neighbors
Controls the number of neighboring minority samples used to create synthetic points.
SMOTE Variants
Borderline-SMOTE
Generates synthetic samples near the decision boundary where misclassification is most likely.
Useful when classes overlap.
SMOTEENN
Combines:
-
SMOTE
-
Edited Nearest Neighbors (ENN)
After generating synthetic samples, ENN removes noisy or ambiguous instances, often leading to cleaner decision boundaries.
SMOTETomek
Combines:
-
SMOTE
-
Tomek Links
Tomek Links identify pairs of very close samples from opposite classes. Removing these pairs helps reduce class overlap after oversampling.
ADASYN
Adaptive Synthetic Sampling generates more synthetic samples for minority instances that are harder to learn, focusing on difficult regions rather than treating all samples equally.
When Should You Use SMOTE?
SMOTE works well when:
-
The minority class has enough samples to learn meaningful patterns.
-
The classes are moderately separable.
-
You need to improve Recall and F1 Score.
-
You are solving classification problems.
When Should You Avoid SMOTE?
SMOTE may not be the best choice when:
-
The minority class contains very few samples.
-
The data contains many outliers.
-
The dataset is extremely noisy.
-
Features are mostly categorical (consider SMOTENC instead).
-
You are working on regression problems.
Best Practices
Split Before Oversampling
Always perform the train-test split first.
Apply SMOTE Only to Training Data
Never oversample the test set.
Use Stratified Splitting
Maintain class proportions during train-test splitting.
Use Proper Metrics
Prefer:
-
Recall
-
Precision
-
F1 Score
-
ROC-AUC
-
PR-AUC
over accuracy.
Tune Model Hyperparameters
Oversampling is only one part of the solution. Combine it with proper feature engineering, feature selection, and hyperparameter tuning.
Consider Pipelines
When using cross-validation, place SMOTE inside an imblearn.pipeline.Pipeline so oversampling happens independently within each training fold, preventing data leakage.
Example:
from imblearn.pipeline import Pipeline
from imblearn.over_sampling import SMOTE
from sklearn.ensemble import RandomForestClassifier
pipeline = Pipeline([
("smote", SMOTE(random_state=42)),
("model", RandomForestClassifier(random_state=42))
])
Common Mistakes
Applying SMOTE before train-test split.
Evaluating using only accuracy.
Oversampling the test dataset.
Ignoring Precision and Recall.
Assuming SMOTE always improves every model.
Using SMOTE on highly noisy datasets without cleaning the data.
Real-World Case Study
Imagine a bank building a loan default prediction model.
Dataset:
100,000 customers
95,000 Paid Loans
5,000 Defaulted Loans
Without SMOTE:
-
High accuracy
-
Poor detection of defaults
With SMOTE:
-
Balanced training data
-
Improved identification of defaulters
-
Higher Recall
-
Better F1 Score
-
Reduced financial risk by catching more likely defaulters
Class imbalance is one of the biggest obstacles in developing reliable machine learning models. Traditional algorithms tend to favor the majority class, making metrics like accuracy misleading in many real-world scenarios.
SMOTE addresses this problem by generating synthetic minority samples instead of duplicating existing ones. This often leads to better learning of minority patterns and significant improvements in Recall and F1 Score. However, SMOTE should be applied carefully—only to the training data, preferably within a machine learning pipeline during cross-validation—to avoid data leakage.
No single technique works best for every dataset. Always compare multiple strategies such as class weighting, random oversampling, undersampling, SMOTE variants, and cost-sensitive learning. Evaluate models using metrics that matter for the problem at hand rather than relying solely on accuracy.
By understanding when and how to use SMOTE, you can build machine learning models that are more robust, fair, and effective in handling real-world imbalanced datasets.
Happy Learning!

