Skip to content

Latest commit

 

History

4 Commits

Folders and files

NameName
Last commit message
Last commit date
 
 
 
 
 
 

Repository files navigation

Credit Card Fraud Detection — Class Imbalance Handling

A systematic comparison of undersampling and oversampling techniques for addressing class imbalance in credit card fraud classification, benchmarked across four machine learning models.


Overview

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.


Dataset

Property Detail
Source Kaggle
Target column Outcome (binary — fraud / legitimate)
Categorical features CardType (MasterCard, Verve, Visa), Domain (International, Local)

Shared Preprocessing Pipeline

Both notebooks follow the same preprocessing steps before applying any resampling:

  1. Feature selection — Chi-squared tests (chi2_contingency) drop categorical columns with p-value > 0.05 (not statistically significant relative to Outcome)
  2. Null handling — Missing values forward-filled (pad)
  3. One-hot encodingCardTypeType_MasterCard, Type_Verve, Type_Visa; DomainDomain_International, Domain_Local
  4. Train/test split — 80/20 split (random_state=42); resampling applied only to training data to prevent data leakage

Techniques Compared

Undersampling (UnderSampling.ipynb)

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

Oversampling (oversampling.ipynb)

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

Classifiers

All resampling techniques are evaluated with the same four classifiers:

  • Decision Tree
  • Random Forest
  • AdaBoost
  • Gradient Boosting

Evaluation Metrics

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.


Dependencies

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

Key Concepts

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.


Related Work

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.

About

Exploring the Effectiveness of Data Balancing Techniques for Credit Card Fraud Detection using Machine Learning

Topics

Resources

Stars

1 star

Watchers

1 watching

Forks

Releases

Packages

Contributors

Languages