Imbalanced data is any classification dataset where one class vastly outnumbers another — fraud, disease, defects, churn — and it breaks the usual assumptions most ML workflows quietly rely on, starting with accuracy itself.
The Reference Dataset for This Section
1000 transactions: 950 legitimate (class 0), 50 fraudulent (class 1) — a 5% positive rate, a realistic order of magnitude for many real fraud/defect/disease problems.
Why Accuracy Lies Here — The Reminder
As covered in Accuracy, a model that predicts "not fraud" for every single transaction achieves 95% accuracy on this dataset while catching zero fraud — a completely useless model with an impressive-looking headline number. This single fact is why every technique in this section exists.
Graphical Intuition
The minority class bar is barely visible next to the majority — exactly why a model can ignore it almost entirely and still score well on accuracy.
The Three Families of Fixes
| Approach | How It Works | Full Note |
|---|---|---|
| Resampling | Change the training data's class balance directly | Oversampling, Undersampling, SMOTE |
| Algorithmic | Make the model itself penalize minority-class mistakes more | Class Weights |
| Evaluation | Use metrics that aren't fooled by imbalance in the first place | Imbalanced Classification Metrics |
Minimal Working Example
from sklearn.linear_model import LogisticRegression
from sklearn.model_selection import train_test_split
from sklearn.metrics import classification_report
X_train, X_test, y_train, y_test = train_test_split(
X, y, test_size=0.2, random_state=42, stratify=y # ALWAYS stratify on imbalanced data
)
model = LogisticRegression(class_weight="balanced") # one of the simplest fixes
model.fit(X_train, y_train)
print(classification_report(y_test, model.predict(X_test)))
Practical Use Cases
- Fraud detection, disease screening, manufacturing defect detection, churn prediction
- Any classification problem where the class you actually care about is the rare one
Common Mistakes
- Reporting accuracy as the headline metric without checking class balance first.
- Forgetting
stratify=yin the train/test split, risking an even more skewed distribution in one of the splits. - Applying resampling to the test set — test data should always reflect the real, naturally imbalanced distribution the model will face in production.
Interview Relevance
Q: "Your fraud model has 95% accuracy on a dataset that's 95% legitimate transactions. What's your first question?" What's the recall on the fraud class specifically — a model could be achieving that accuracy by predicting "not fraud" for nearly everyone, catching little to no actual fraud, which the accuracy number alone would completely hide.
Practice Question
You're building a manufacturing defect detector where only 2% of items are defective. Name three distinct approaches (one from each family above) you'd consider, and explain the tradeoff each involves.