A systematic comparison of undersampling and oversampling techniques for addressing class imbalance in credit card fraud classification, benchmarked across four machine learning models.
Credit card fraud datasets are severely imbalanced — legitimate transactions far outnumber fraudulent ones. A naive classifier trained on such data will simply predict "not fraud" for almost everything and still achieve high accuracy, while missing the actual fraud cases it needs to catch.
This repository tackles that problem from two angles: undersampling (reducing the majority class) and oversampling (augmenting the minority class). Both strategies are evaluated across four classifiers to identify which combination performs best for fraud detection.
| Property | Detail |
|---|---|
| Source | Kaggle |
| Target column | Outcome (binary — fraud / legitimate) |
| Categorical features | CardType (MasterCard, Verve, Visa), Domain (International, Local) |
Both notebooks follow the same preprocessing steps before applying any resampling:
- Feature selection — Chi-squared tests (
chi2_contingency) drop categorical columns with p-value > 0.05 (not statistically significant relative toOutcome) - Null handling — Missing values forward-filled (
pad) - One-hot encoding —
CardType→Type_MasterCard,Type_Verve,Type_Visa;Domain→Domain_International,Domain_Local - Train/test split — 80/20 split (
random_state=42); resampling applied only to training data to prevent data leakage
Reduces the majority (non-fraud) class to balance the dataset. No new data is created — real samples are discarded.
| Technique | How it works |
|---|---|
| Random Undersampling | Randomly removes majority class samples until classes are balanced |
| NearMiss-1 | Keeps majority samples closest (on average) to the k nearest minority samples |
| NearMiss-2 | Keeps majority samples closest (on average) to the k farthest minority samples |
| NearMiss-3 | For each minority sample, retains a fixed number of the nearest majority samples |
| Edited Nearest Neighbours (ENN) | Removes majority samples misclassified by their nearest neighbours — a gentle boundary-cleaning approach |
| Tomek Links | Removes the majority-class member of each borderline majority-minority pair |
Augments the minority (fraud) class to balance the dataset. No real data is lost — synthetic or duplicated samples are added.
| Technique | How it works |
|---|---|
| Random Oversampling | Duplicates existing minority class samples at random |
| SMOTE | Generates synthetic minority samples by interpolating between existing ones and their k-nearest neighbours |
| ADASYN | Like SMOTE, but generates more synthetic samples in regions where the classifier struggles — adaptive density-based |
| Borderline-SMOTE | Variant of SMOTE that focuses synthesis on minority samples near the decision boundary |
All resampling techniques are evaluated with the same four classifiers:
- Decision Tree
- Random Forest
- AdaBoost
- Gradient Boosting
Each combination is assessed on:
| Metric | What it measures |
|---|---|
| Accuracy | Overall correct predictions |
| Precision | Of predicted fraud cases, how many were actually fraud |
| Recall | Of all actual fraud cases, how many were caught |
| F1-Score | Harmonic mean of Precision and Recall |
In fraud detection, Recall is the most critical metric — a missed fraud (false negative) is far more costly than a false alarm. Accuracy alone is misleading on imbalanced data.
pip install pandas numpy scikit-learn imbalanced-learn seaborn matplotlib scipy| Library | Purpose |
|---|---|
pandas, numpy |
Data manipulation |
scikit-learn |
Models, preprocessing, metrics |
imbalanced-learn |
All resampling techniques (SMOTE, NearMiss, ENN, etc.) |
scipy |
Chi-squared feature selection |
seaborn, matplotlib |
Visualization |
Undersampling vs. Oversampling — which is better? Neither approach is universally superior. Undersampling is faster and avoids synthetic data but discards real information. Oversampling retains all original data but risks overfitting when duplicating samples (Random Oversampling) or introducing noise along the wrong boundaries (naive SMOTE). The best choice depends on the dataset size and the degree of imbalance — which is exactly what these experiments investigate.
Why test multiple techniques? Each method makes different assumptions about where the decision boundary lies. NearMiss variants are boundary-aware but noise-sensitive. ENN and Tomek Links are conservative cleaning methods. SMOTE synthesises uniformly while ADASYN and Borderline-SMOTE concentrate effort on hard-to-classify regions. Running all of them on the same models and dataset reveals which assumptions hold for this specific fraud problem.
This project is part of broader research on credit card fraud detection using ensemble methods, data balancing strategies, and explainability. The published research (IEEE ECCE 2025) investigates SHAP-based explainability applied to fraud detection models.