Author: Juan Francisco Orona Repository: credit_card_fraud_detection_ML_model
This project builds and compares supervised Machine Learning models to predict whether a credit card transaction is fraudulent. It was developed as part of the Ironhack Data Analytics ML Project Kickoff, covering the full ML workflow: data cleaning and EDA, feature engineering, model building, handling class imbalance, hyperparameter tuning, and model evaluation.
- Source: Credit Card Fraud Detection Dataset — Kaggle
- Size: ~1M transactions, no missing values or duplicates
- Target variable:
fraud(1 = fraudulent, 0 = legitimate) - Features:
distance_from_home— distance from home where the transaction happeneddistance_from_last_transaction— distance from the previous transactionratio_to_median_purchase_price— ratio of the purchase price to the median purchase pricerepeat_retailer— whether the transaction was made at a retailer used beforeused_chip— whether the transaction was made using a chipused_pin_number— whether a PIN was usedonline_order— whether the transaction was an online order
The dataset is moderately imbalanced (~91% legitimate vs ~9% fraud).
📊 Project presentation (Google Slides)
- Checked for nulls and duplicates (none found)
- Reviewed feature distributions and outliers via IQR
- Explored correlations between features and the target
- 80/20 split,
random_state=20
- Scaling: compared
MinMaxScaler(normalization) vsStandardScaler(standardization), fit only on training data to avoid data leakage - Feature selection: evaluated the impact of dropping the low-correlation
repeat_retailerfeature
| Model | Notes |
|---|---|
| K-Nearest Neighbors (KNN) | Strong performer; benefited significantly from feature scaling |
| Logistic Regression | Struggled with recall on the imbalanced data, even after scaling |
| Decision Tree (winning model) | Near-perfect performance out of the box; used for feature importance analysis and hyperparameter tuning |
Applied and compared two resampling techniques on the training set:
- SMOTE (Synthetic Minority Oversampling)
- Random Oversampling
Both techniques were evaluated against the baseline (scaled, non-resampled) models to study the precision/recall trade-off.
- Grid Search and Randomized Search applied to the Decision Tree (
max_depth,min_samples_split,min_samples_leaf), optimizing for F1-score with 5-fold cross-validation
Models were evaluated using Confusion Matrix, Accuracy, Precision, Recall, and F1-score, with an emphasis on Recall as the priority metric — in fraud detection, missing a fraudulent transaction (false negative) is typically costlier than flagging a legitimate one (false positive).
- Decision Tree is the winning model. It achieved near-perfect classification (F1-score ≈ 0.9999) right out of the box, outperforming both KNN and Logistic Regression, and required no resampling or extensive feature engineering to reach top performance.
- Feature importance showed the tree relies mainly on
ratio_to_median_purchase_price(42.5%) andonline_order(23.4%), withused_pin_numberanddistance_from_homealso contributing meaningfully — explaining why a relatively shallow tree (depth 7) achieves such strong results. repeat_retailerconsistently showed the lowest predictive value across all models and could be removed with minimal impact on performance.- Hyperparameter tuning (Grid Search and Randomized Search) confirmed the Decision Tree's default configuration was already near-optimal — tuning validated rather than improved the original model.
- KNN was the runner-up, achieving strong results (F1-score ≈ 0.99) after feature scaling, but did not match the Decision Tree's performance.
- Logistic Regression consistently underperformed on Recall, even after standardization and feature selection, indicating the model itself — not the preprocessing — was the limiting factor.
- Resampling trade-off: for KNN and Logistic Regression, Random Oversampling and SMOTE both increased Recall (fewer missed frauds) at the cost of Precision (more false positives). Since the Decision Tree already performed near-perfectly, resampling wasn't necessary for the winning model.
- Clone the repository:
git clone https://github.com/fran26m/credit_card_fraud_detection_ML_model.git cd credit_card_fraud_detection_ML_model - Install the required libraries:
pip install pandas numpy matplotlib seaborn scikit-learn imbalanced-learn jupyter
- Download the dataset from Kaggle and place
card_transdata.csvin your project folder (update the file path in the notebook's first data-loading cell to match its location on your machine). - Launch Jupyter Notebook and open
credit_card_fraud.ipynb:jupyter notebook credit_card_fraud.ipynb
- Run the cells in order from top to bottom.
credit_card_fraud.ipynb— full analysis notebook: EDA, feature engineering, model training, imbalance handling, hyperparameter tuning, and evaluationREADME.md— this file
- Python, Jupyter Notebook
- pandas, numpy
- scikit-learn (KNN, Logistic Regression, Decision Tree, GridSearchCV, RandomizedSearchCV, preprocessing, metrics)
- imbalanced-learn (SMOTE, RandomOverSampler)
- matplotlib, seaborn