A complete pipeline for EDA and model training to detect fraudulent credit card transactions using CatBoost and Optuna.
The goal of this project is to detect fraudulent credit card transactions using data analysis, class balancing techniques, and advanced machine learning models. The dataset comes from Credit Card Fraud Detection, containing 284,807 transactions, of which only 492 are frauds.
| Class | Precision | Recall | F1-Score | Support |
|---|---|---|---|---|
| Legitimate (0) | 0.9997 | 0.9996 | 0.9996 | 56864 |
| Fraudulent (1) | 0.7822 | 0.8061 | 0.7940 | 98 |
| Accuracy | 0.9993 | 56962 | ||
| Macro avg | 0.8909 | 0.9029 | 0.8968 | 56962 |
| Weighted avg | 0.9993 | 0.9993 | 0.9993 | 56962 |
The dataset used in this project is the Credit Card Fraud Detection dataset, originally released by European cardholders in 2013. It contains real-world anonymized transactions made by credit cards over a two-day period.
- Total rows: 284,807 transactions
- Fraudulent transactions: 492 (≈0.172%)
- Non-fraudulent transactions: 284,315
- Features: 30 columns (28 anonymized +
Time+Amount) - Target column:
Class0— Legitimate transaction1— Fraudulent transaction
| Column | Description |
|---|---|
Time |
Seconds elapsed between the transaction and the first transaction in the dataset |
Amount |
Transaction amount |
V1–V28 |
PCA-transformed features (anonymized for privacy) |
Class |
Target variable (1 = fraud, 0 = non-fraud) |
The features
V1throughV28are the result of a PCA transformation and have no direct interpretable meaning. TheTimeandAmountfeatures were not transformed and may require preprocessing.
This is a highly imbalanced classification problem. Fraudulent transactions make up less than 0.2% of the dataset, which makes it crucial to apply proper resampling or class weighting techniques during model training.
.
├── data/
│ └── creditcard.csv # Raw dataset
├── notebooks/
│ ├── 00-eda.ipynb # Exploratory Data Analysis
│ └── 10-model-training.ipynb # Model training and hyperparameter tuning
├── models/
│ └── catboost_fraud_model.cbm # Final trained model
├── requirements.txt
└── README.md
- Distribution analysis
- Outlier detection for
Amount - Correlation heatmap (
sns.heatmap) - Class imbalance inspection
- Log-transformation of
Amount
- SMOTE (Synthetic Minority Oversampling Technique) - didn't work
- Stratified undersampling (optional) - didn't work
scale_pos_weightparameter in CatBoost
- CatBoost Classifier (efficient, handles categorical features)
- Hyperparameter tuning with Optuna
- Cross-validation using ROC AUC
- Optimal threshold selection based on F1-score
- Precision-Recall curve visualization
- Error analysis (confusion matrix, FP/FN inspection)
- Feature importance visualization
pandas,numpymatplotlib,seabornscikit-learncatboostoptunaimblearn- for SMOTE, didn't work
- Install the dependencies:
pip install -r requirements.txt
- Open and run the notebooks in the notebooks/ directory in order.
- Improved precision for the minority (fraud) class ≈ 0.78
- AUC ≈ 0.99
- Significant F1-score improvement using the optimized threshold
- Final model saved at models/catboost_fraud_model.cbm
- Try LightGBM or XGBoost for comparison
- Add explainability (e.g. SHAP, LIME)
- Deploy the model with Gradio or FastAPI
- Dockerized deployment and inference with Triton