- Overview
- Problem Statement
- Dataset Description
- Objective
- Project Directory
- Setup
- Model Report
- Architecture
- Screenshots
- Future Work
- Project Evaluation
Fraud detection is a critical aspect of ensuring the integrity and trustworthiness of transactions in industries like finance, e-commerce, and banking. This project aims to build a machine learning model that can accurately classify transactions as either legitimate or fraudulent.
The goal of the project is to create an automated fraud detection system that can predict fraudulent transactions based on historical data.
With the rise in online transactions, financial institutions face an increasing challenge of identifying fraudulent transactions. Fraudulent activities lead to significant financial losses, reputation damage, and legal consequences.
This project addresses the problem by building a machine learning model capable of automatically classifying transactions as fraudulent or non-fraudulent using historical transaction data.
kaggle dataset: fraud-detection
The dataset for this project is fraudTrain.csv, which contains transaction data with various features. These features are used to train the model to identify patterns associated with fraudulent behavior. The dataset includes:
- Features: Various transaction attributes, including transaction amount, customer data, and timestamps.
- Labels: The target column, where
1indicates a fraudulent transaction and0indicates a legitimate transaction.
The main objective of this project is to develop a machine learning model to automatically detect fraudulent transactions. The project consists of the following steps:
- Data Preprocessing: Clean and prepare the dataset for modeling.
- Model Development: Train and fine-tune various machine learning models.
- Model Evaluation: Evaluate the model’s performance using appropriate classification metrics.
- Deployment: Deploy the model for real-time fraud detection.
The project directory is structured as follows:
├── data # contains data (Download from kaggle)
│ ├── fraudTest.csv
│ ├── fraudTrain.csv
├── notebooks
│ ├── eda
│ │ ├── eda.ipynb # Exploratory Data Analysis notebook
│ ├── modeling
│ │ ├── train_v6.py # Hyperparameter tuning script
│ └── requirements.txt
├── output # output of model training script is saved here
├── services
│ ├── fraud_detection
│ │ ├── api
│ │ │ ├── api.py
│ │ │ ├── models # dir for models for deployment
│ │ │ │ └── best_model.pkl # add from release
│ │ │ ├── requirements.txt
│ │ │ └── run.sh # script for running using python env
│ │ ├── Dockerfile
│ │ ├── integration # Integration test scripts
│ │ └── tests # API unit testing scripts
│ └── frontend # Streamlit application to consume the
├── docker-compose.yaml # docker compose file
├── kind.config # kind config
├── Makefile # make file
├── manifest # dir contains kubernetes manifest
│ ├── api
│ └── frontend
├── README.md
-
Create a Virtual Environment
- Run the following command to create a virtual environment. Replace
myenvwith your desired environment name:
python -m venv myenv
- Run the following command to create a virtual environment. Replace
-
Activate the Virtual Environment
- On Windows:
myenv\Scripts\activate
- On macOS/Linux:
source myenv/bin/activate
- On Windows:
-
Install Dependencies
- Install the required packages by running:
pip install -r notebooks/requirements.txt
-
(Optional) Register Kernel for VS Code
- To register the virtual environment as a Jupyter kernel for VS Code:
python -m ipykernel install --user --name=myenv --display-name "Python (myenv)"
-
Add kaggle keys to .kaggle Refer christianjmills post to get more info on how to add kagle token to your system
nano ~/.kaggle/kaggle.json{ "username": "username", "key":"key_xsfasfaf" } -
Download Dataset Files
- After setting up the virtual environment, update the Makefile with your environment name and run:
make activate_env make setup_mldev make download_data
-
Update File Paths
- Ensure the file paths in your notebooks or scripts are correctly pointing to the
/datadirectory if paths have changed.
- Ensure the file paths in your notebooks or scripts are correctly pointing to the
-
Run Exploratory Data Analysis (EDA)
- Navigate to the EDA folder and run the EDA notebook:
cd notebooks/eda jupyter notebook eda.ipynb -
Train the Model
- Navigate to the modeling directory and execute the model training script:
cd notebooks/modeling python train_v6.py
-
Download and Add the Model
- Download the model file from the release section or after training, you can find the model in the output directory.
- Place the model file in the following directory:
services/fraud_detection/api/models
-
Update the Model Path
- Update the
MODEL_FILEvariable inservices/fraud_detection/api/api.pyto point to the downloaded model file.
- Update the
-
Running the Application This project provides three ways to run the application:
- Using Docker Compose
- Using Python Virtual Environment
- Using Kubernetes with Kind
-
Install Docker and Docker Compose
- Make sure you have Docker and Docker Compose installed.
-
Run Docker Compose
- Start the application using the following command:
docker-compose up --build
-
Stop Docker Compose
- Start the application using the following command:
docker-compose down
-
Install Required Tools
- Install Docker, Kind (Kubernetes in Docker), and Make on your machine.
-
Initialize Kind Cluster
- Run the following command to initialize the Kind cluster:
make kind-init
-
Deploy All Development Services
- Deploy the necessary development services:
make dev_deploy_all
-
Test the API
- Wait for both the API and frontend services to be in a running state. You can check the status with:
kubectl get po
-
Update API_URL to
API_URL = "http://localhost:30007"inservices/fraud_detection/integration/test_integration.py. else integration tests will throw errors -
Run integration tests to ensure the API is working:
make dev_api_integation_test
-
Access the Application
- After deployment, access the application at http://localhost.
-
Delete the Cluster
- To clean up resources and delete the Kind cluster:
make kind-delete
-
Build and Deploy Services
- After initializing the Kind cluster, you can build and deploy all services with:
make dev_deploy_all_full
-
Run Make Help
- For more available commands, run:
make help
[TODO]: need to update
Once the model is trained, a detailed model evaluation report will be provided. The report includes:
- Model selection process
- Hyperparameter tuning results
- Performance metrics (accuracy, precision, recall, F1-score)
- Model confusion matrix
You can view the detailed report here.
The architecture of the system involves the following key components:
- Data Preprocessing: Data is cleaned and transformed into a format suitable for model training.
- Machine Learning Model: A model is trained using the dataset to detect fraudulent transactions.
- Model Evaluation: The model is evaluated using classification metrics to ensure it works accurately.
- Deployment: The model is containerized and deployed using Docker and Kubernetes for scalability.
Below are some screenshots of the project:
Deployment
-> deploying services and testing
for detailed log refer to Deployment terminal logs
Future enhancements for this project could include:
- Improving Accuracy: Experimenting with more advanced algorithms such as deep learning models.
- Real-Time Fraud Detection: Implementing real-time fraud detection capabilities.
- Data Augmentation: Collecting more data to further improve model performance.
- Ensemble Methods: Using ensemble learning techniques (e.g., Random Forest, XGBoost) for better prediction results.