How do you handle class imbalance in ML?
Written by the mlprep team · Updated
You're building a fraud detection model. Only 0.1% of transactions are fraud. How do you handle this class imbalance?
First: recognize what the problem actually is#
A model that always predicts "not fraud" achieves 99.9% accuracy. Accuracy is useless here. Your real objectives are precision and recall on the minority class — and the trade-off between them (F1, F-β, or AUC-PR).
The core problem: with a 1:1000 ratio, the model's loss is dominated by the majority class, and it learns to ignore the minority class to minimize total loss.
Approach 1: Loss-level intervention#
Class-weighted loss: multiply the minority class loss by a weight proportional to the class imbalance. If 0.1% fraud:
class_weight = {0: 1, 1: 999} # or compute from dataThe model now sees 999× more penalty for missing fraud than for a false alarm. Simple, no data modification, fully compatible with cross-validation.
Focal loss: additionally downweights easy-to-classify examples so hard fraud cases get proportionally more gradient. See focal loss question for mechanics.
Approach 2: Data-level intervention#
Oversampling (SMOTE): Synthetic Minority Oversampling TEchnique. For each minority example, find k nearest neighbors in feature space and create new synthetic examples along the line segments between them. Unlike naive duplication, creates novel examples that interpolate the minority distribution.
synthetic = x_i + λ·(x_neighbor - x_i) where λ ~ Uniform(0,1)
Undersampling: randomly drop majority examples to balance classes. Loses information — only use if the dataset is very large and the majority class is redundant. Tomek links (remove majority examples near the decision boundary) is a smarter variant.
Warning on resampling: resample only on training data. Never on validation or test. And resampling changes the prior — your model's output probabilities are no longer calibrated to the real-world 0.1% rate.
Approach 3: Threshold adjustment#
By default, classifiers output 0.5 as the decision threshold. But with 1:1000 imbalance, the optimal threshold for your business objective is almost certainly much lower than 0.5. Tune the threshold on validation data using your actual metric (F1, precision@recall, etc.) rather than resampling at all.
This is often the cleanest solution: train on imbalanced data (maybe with class weights), tune the threshold post-hoc on the validation set.
Approach 4: Anomaly detection framing#
If fraud is truly rare and the fraud distribution is highly variable, reframe as anomaly detection. Train only on majority class examples and flag anything that deviates significantly. Methods: Isolation Forest, One-Class SVM, autoencoders. Works best when you have very few labeled fraud examples.
What I'd actually do for fraud detection#
- Use class-weighted cross-entropy or focal loss — no data modification needed.
- Tune decision threshold on validation set using F-β score (where β > 1 to weight recall over precision, since missing fraud is costly).
- Evaluate on AUC-PR (precision-recall curve), not AUC-ROC — AUC-ROC is misleading under severe imbalance.
- If still underperforming on the minority class, try SMOTE on the training fold only.
- Check if you need calibration post-hoc for probability outputs.
Interview red flags#
- Reporting accuracy on a 0.1% positive-rate problem.
- Oversampling or applying SMOTE before the train/test split, which leaks synthetic copies into evaluation.
- Resampling to balance classes and then treating the model's scores as calibrated probabilities.
- Leaving the decision threshold at 0.5 instead of choosing it from business costs of false positives and false negatives.
The key takeaway unlocks after you reveal an expert answer.
Key Takeaway
Class imbalance approaches: (1) class-weighted loss — simplest, no data modification; (2) SMOTE oversampling — synthetic minority examples, only on training data, breaks calibration; (3) threshold tuning — train normally, adjust decision boundary post-hoc; (4) anomaly detection if minority class is tiny and variable. Use AUC-PR not AUC-ROC for imbalanced evaluation. Accuracy is useless at 1:1000 ratios.
Follow-up Questions to Test Yourself
Interviewers often pivot here once you cover the core concept.
- Why is AUC-ROC misleading for imbalanced classification, and why is AUC-PR better?
- SMOTE creates interpolated examples between minority class neighbors. What can go wrong with this assumption?
- How do you recalibrate a model's predicted probabilities after oversampling to reflect the true class prior?
How confident do you feel explaining this?
Track your understanding or flag it to revisit before your next loop.
Related Questions
Walk me through feature engineering for tabular data
How do you encode categorical features? Compare one-hot, target encoding, and embeddings.
How do you approach feature selection?
What is bootstrapping and when should you use it over analytical methods?