This repository contains code for detecting credit card fraud using a neural network model. The dataset used is highly imbalanced, and the code demonstrates techniques to handle this imbalance using undersampling and SMOTE (Synthetic Minority Over-sampling Technique).
Credit card fraud detection is a critical application of machine learning. This project showcases how to handle imbalanced datasets and build a neural network model to detect fraudulent transactions.
The dataset used is the Credit Card Fraud Detection dataset from Kaggle. It contains transactions made by credit cards in September 2013 by European cardholders. The dataset has 284,807 transactions, with only 492 being fraudulent (0.172% of all transactions).
- Scaling: The
Amount
andTime
features are scaled usingRobustScaler
to reduce the influence of outliers. - Splitting: The dataset is split into training and testing sets.
- Handling Imbalance:
- Undersampling: Reduces the number of majority class instances to balance the dataset.
- SMOTE: Generates synthetic minority class instances to balance the dataset.
Two models are built using Keras:
- Undersampling Model: Trained on the undersampled dataset.
- Oversampling Model (SMOTE): Trained on the SMOTE-generated dataset.
Both models use a simple neural network architecture with:
- Input layer
- Two hidden layers with ReLU activation
- Output layer with sigmoid activation
The models are evaluated using a confusion matrix to assess their performance on detecting fraudulent transactions.
To run this code, you need to have Python 3 and the required packages installed. Follow these steps:
-
Clone the repository:
git clone https://github.com/yourusername/credit-fraud-detector.git
-
Navigate to the project directory:
cd credit-fraud-detector
-
Create and activate a virtual environment (optional but recommended):
python -m venv venv source venv/bin/activate # On Windows use `venv\Scripts\activate`
-
Install the required packages:
pip install -r requirements.txt
-
Place the
creditcard.csv
file in the project directory. -
Run the script:
python credit_fraud_detector.py
This project is licensed under the MIT License - see the LICENSE file for details.