A Machine Learning approach to detect fraudulent credit card transactions using Random Forest Classifier
- Project Overview
- Features
- Technologies Used
- Project Structure
- Installation
- Data
- Usage
- Model Performance
- Analysis & Visualizations
- Results
- Author
This project implements a Machine Learning solution for detecting fraudulent credit card transactions. Using a comprehensive dataset of credit card transactions, we've built a Random Forest Classifier that can accurately identify potentially fraudulent activities while minimizing false positives.
Credit card fraud is a significant concern in the financial industry, causing billions of dollars in losses annually. Traditional rule-based systems often fail to adapt to new fraud patterns. This project leverages machine learning to:
- Detect fraudulent transactions in real-time
- Reduce false positives to maintain customer satisfaction
- Provide interpretable results for fraud investigation teams
- High Accuracy: Achieves excellent performance metrics with Random Forest
- Comprehensive Analysis: Detailed exploratory data analysis and visualization
- Feature Importance: Identifies the most important factors in fraud detection
- Performance Metrics: ROC curves, confusion matrices, and classification reports
- Interactive Visualizations: Beautiful plots using Matplotlib and Seaborn
- Scalable: Easily deployable for real-world applications
Fraud-Detection-ML/
│
├── data/ # Data directory (ignored in git)
│ ├── .gitkeep # placeholder to keep the folder
│ └── sample_small.csv # small demo file (optional, tracked in git)
│
├── notebook/
│ └── fraud_detection.ipynb # Main Jupyter notebook with analysis
│
├── requirements.txt # Python dependencies
└── README.md # Project documentation- Python 3.7 or higher
- pip package manager
-
Clone the repository
git clone https://github.com/Abdou-bnm/Fraud-Detection-ML.git cd Fraud-Detection-ML -
Create a virtual environment (recommended)
python -m venv fraud_detection_env
Activate the environment
- On Windows:
fraud_detection_env\Scripts\activate
- On macOS/Linux:
source fraud_detection_env/bin/activate
- On Windows:
-
Install required packages
pip install -r requirements.txt
-
Launch Jupyter Notebook
jupyter notebook
-
Open the analysis notebook Navigate to:
notebook/fraud_detection.ipynb
This project uses the Credit Card Fraud Detection Dataset 2023 from Kaggle.
- Dataset link: Credit Card Fraud Detection Dataset 2023
- Size: ~550,000 transactions (~150 MB)
- Source: Kaggle (uploaded by nelgiriyewithana)
- Note: The dataset is not included in this repository due to size and licensing. Please download it yourself if you’d like to run the full analysis.
This dataset contains credit card transactions made by European cardholders in the year 2023.
It comprises over 550,000 records, and the data has been anonymized to protect cardholder identities.
The primary objective of the dataset is to support the development of fraud detection algorithms and models that can identify potentially fraudulent transactions.
- Download the dataset from the Kaggle link above.
- Move the CSV file into the
data/folder.
Expected structure:
Fraud-Detection-ML/
└── data/
└── creditcard_2023.csv # Kaggle dataset file (not tracked in git)
👉 For quick tests, the repo includes small sample_*.csv files (kept in git) so you can try out the notebooks without downloading the full dataset.
For lightweight testing, a small demo dataset is already included in this repository:
- File:
data/sample_small.csv - Size: first 5,000 rows of the full dataset
This sample is small enough to be tracked in git and lets you run the notebooks instantly, even if you don’t download the full dataset from Kaggle.
-
Data Loading & Exploration
- Load the credit card dataset (from
data/creditcard_2023.csvordata/sample_small.csv) - Perform exploratory data analysis
- Check for missing values and data quality
- Load the credit card dataset (from
-
Data Preprocessing
- Feature selection and engineering
- Data scaling using
StandardScaler - Train-test split for model validation
-
Model Training
- Train a Random Forest Classifier
- Perform cross-validation
- Hyperparameter optimization
-
Model Evaluation
- Generate predictions on the test set
- Create confusion matrix and classification report
- Plot ROC curve and calculate AUC
-
Visualization & Insights
- Feature importance analysis
- Correlation matrix visualization
- Performance metrics visualization
| Metric | Score |
|---|---|
| Accuracy | ~98.7% |
| Precision | High precision for fraud cases |
| Recall | Excellent fraud capture rate |
| F1-Score | Balanced performance metric |
| AUC-ROC | Near-perfect classification |
- 5-Fold Cross-Validation implemented
- Consistent performance across all folds
- Robust model with minimal overfitting
The project includes comprehensive visualizations:
- Confusion Matrix: Visual representation of model predictions
- ROC Curve: Model discrimination capability
- Feature Importance: Most influential features for fraud detection
- Correlation Heatmap: Feature relationships and dependencies
- Classification Report: Detailed performance metrics
- High Accuracy: The Random Forest model achieves exceptional performance in detecting fraudulent transactions
- Feature Insights: Certain transaction features are significantly more important for fraud detection
- Balanced Performance: The model maintains low false positive rates while capturing most fraud cases
- Scalability: The approach can be scaled for real-time fraud detection systems
- Cost Reduction: Minimize financial losses from fraudulent transactions
- Risk Mitigation: Proactive fraud prevention
- Customer Satisfaction: Reduced false positives mean fewer legitimate transactions blocked
- Real-time Detection: Fast prediction capability for immediate action