Skip to content

Latest commit

 

History

1 Commit

Folders and files

NameName
Last commit message
Last commit date
 
 
 
 

Repository files navigation

Credit Card Fraud Detection — Machine Learning Project

Author: Juan Francisco Orona Repository: credit_card_fraud_detection_ML_model

Project Overview

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.

Dataset

  • 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 happened
    • distance_from_last_transaction — distance from the previous transaction
    • ratio_to_median_purchase_price — ratio of the purchase price to the median purchase price
    • repeat_retailer — whether the transaction was made at a retailer used before
    • used_chip — whether the transaction was made using a chip
    • used_pin_number — whether a PIN was used
    • online_order — whether the transaction was an online order

The dataset is moderately imbalanced (~91% legitimate vs ~9% fraud).

Presentation

📊 Project presentation (Google Slides)

Methodology

1. Data Cleaning & EDA

  • Checked for nulls and duplicates (none found)
  • Reviewed feature distributions and outliers via IQR
  • Explored correlations between features and the target

2. Train/Test Split

  • 80/20 split, random_state=20

3. Feature Engineering

  • Scaling: compared MinMaxScaler (normalization) vs StandardScaler (standardization), fit only on training data to avoid data leakage
  • Feature selection: evaluated the impact of dropping the low-correlation repeat_retailer feature

4. Models Trained

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

5. Handling Class Imbalance

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.

6. Hyperparameter Tuning

  • 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

7. Model Evaluation

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).

Key Findings

  • 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%) and online_order (23.4%), with used_pin_number and distance_from_home also contributing meaningfully — explaining why a relatively shallow tree (depth 7) achieves such strong results.
  • repeat_retailer consistently 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.

How to Run

  1. Clone the repository:
    git clone https://github.com/fran26m/credit_card_fraud_detection_ML_model.git
    cd credit_card_fraud_detection_ML_model
  2. Install the required libraries:
    pip install pandas numpy matplotlib seaborn scikit-learn imbalanced-learn jupyter
  3. Download the dataset from Kaggle and place card_transdata.csv in your project folder (update the file path in the notebook's first data-loading cell to match its location on your machine).
  4. Launch Jupyter Notebook and open credit_card_fraud.ipynb:
    jupyter notebook credit_card_fraud.ipynb
  5. Run the cells in order from top to bottom.

Repository Contents

  • credit_card_fraud.ipynb — full analysis notebook: EDA, feature engineering, model training, imbalance handling, hyperparameter tuning, and evaluation
  • README.md — this file

Tools & Libraries

  • Python, Jupyter Notebook
  • pandas, numpy
  • scikit-learn (KNN, Logistic Regression, Decision Tree, GridSearchCV, RandomizedSearchCV, preprocessing, metrics)
  • imbalanced-learn (SMOTE, RandomOverSampler)
  • matplotlib, seaborn

About

No description, website, or topics provided.

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages