This is a machine learning project I built to detect fraudulent credit card transactions in real-time. It uses a Random Forest model hooked up to a Flask API, and includes a web interface where you can test out transactions to see if the model flags them as fraud.
The system is pretty straightforward:
- I trained a few machine learning models (using GridSearchCV for tuning) and saved the best one.
- There's a local MySQL database setup to store the prediction logs for analytics.
- The backend is a Flask app (
app.py) that handles incoming transaction data and runs it through the model. - The frontend (
templates/index.html) is a simple UI for submitting transaction data manually.
app.py: Main Flask application and prediction logicsql/: SQL scripts to create the database tablesmodels/: Where the trained.pklmodels are storednotebooks/: Jupyter notebooks I used for EDA and model trainingtemplates/: HTML files for the web interfacerender.yaml: Configuration for easy deployment on Render
- Clone the repo
git clone https://github.com/avyasaini/Credit-Card-Fraud-Detection.git
cd Credit-Card-Fraud-Detection- Install dependencies
pip install -r requirements.txt- Run the app locally
python app.pyThe app will run on http://127.0.0.1:5000.
You can also test the /predict endpoint directly using Postman or curl. Send a POST request with JSON data like this:
{
"amt": 281.06,
"city_pop": 885,
"lat": 35.9946,
"long": -81.7266,
"merch_lat": 36.430124,
"merch_long": -81.179483,
"unix_time": 1325466397,
"category": "grocery_pos"
}The Random Forest model performed really well on the validation set, achieving an F1-Score of around 0.99. I used balanced class weights to deal with the heavily imbalanced dataset (since most transactions aren't fraud).
If you have any questions or feedback about this project, feel free to open an issue in the repository!