Plutus is a machine learning-powered fraud detection system that provides real-time credit card transaction analysis with explainable AI insights. The system uses XGBoost for fraud prediction and SHAP (SHapley Additive exPlanations) for model interpretability.
- Real-time Fraud Detection: Fast API-based prediction endpoint for instant transaction analysis
- Explainable AI: SHAP-based feature importance to understand why transactions are flagged
- Interactive Dashboard: Streamlit-based web interface for easy interaction and visualization
- Decision Logic: Three-tier decision system (ALLOW, REVIEW, BLOCK) based on fraud probability
- Synthetic Data Generation: Built-in data generator for testing and development
- Production-Ready API: FastAPI-based REST API with proper error handling
Plutus/
├── api/
│ ├── app.py # FastAPI application
│ └── artifacts/ # Trained model artifacts
│ ├── plutus_xgb.pkl # XGBoost model
│ ├── encoders.pkl # Label encoders for categorical features
│ ├── feature_list.pkl # Feature order list
│ └── threshold.pkl # Optimal decision threshold
├── dashboard.py # Streamlit dashboard
├── data_preprocess.py # Data preprocessing pipeline
├── model_training.ipynb # Model training notebook
├── synthetic_data_generator.py # Synthetic transaction data generator
├── plutus_transactions.csv # Transaction dataset
├── requirements.txt # Python dependencies
└── README.md # This file
- Python 3.8 or higher
- pip or conda package manager
-
Clone or navigate to the project directory:
cd Plutus -
Create a virtual environment (recommended):
python -m venv venv source venv/bin/activate # On Windows: venv\Scripts\activate
-
Install dependencies:
pip install -r requirements.txt
If you need to generate synthetic transaction data:
python synthetic_data_generator.pyThis will create plutus_transactions.csv with:
- 150,000 transactions from 5,000 users
- ~1.5% fraud rate
- Realistic features including temporal patterns, merchant categories, and behavioral flags
python data_preprocess.pyThis script:
- Loads the transaction dataset
- Encodes categorical features (merchant_category, payment_method, country_code)
- Splits data into train/validation sets (80/20, stratified)
- Saves encoders and feature list to artifacts
Open and run model_training.ipynb:
-
Train XGBoost Classifier:
- Uses class weights to handle imbalanced data
- 500 estimators, max depth 6
- Early stopping based on validation set
-
Find Optimal Threshold:
- Uses Precision-Recall curve
- Selects threshold with recall ≥ 0.8
- Saves threshold for production use
-
Evaluate Model:
- Generates classification report
- Tests multiple threshold values
Important: After training, move the artifacts to the API directory:
mv plutus_xgb.pkl api/artifacts/
mv encoders.pkl api/artifacts/
mv feature_list.pkl api/artifacts/
mv threshold.pkl api/artifacts/From the project root directory:
cd api
uvicorn app:app --reload --host 0.0.0.0 --port 8000Or from the root:
uvicorn api.app:app --reload --host 0.0.0.0 --port 8000The API will be available at http://localhost:8000
Once the server is running, visit:
- Interactive API Docs: http://localhost:8000/docs
- Alternative Docs: http://localhost:8000/redoc
GET /healthResponse:
{
"status": "Plutus is alive"
}POST /predict
Content-Type: application/jsonRequest Body:
{
"timestamp": "2025-01-15T14:30:00",
"amount": 450.50,
"merchant_category": "electronics",
"payment_method": "card",
"country_code": "US",
"txn_count_1h": 2,
"txn_count_24h": 15,
"avg_amount_7d": 120.0,
"amount_deviation": 330.5,
"time_since_last_txn": 45.0,
"is_night": 0,
"is_weekend": 0,
"new_merchant_flag": 1,
"geo_jump": 0,
"high_amount_flag": 1
}Response:
{
"fraud_probability": 0.6234,
"fraud_prediction": 1,
"decision": "BLOCK",
"top_risk_factors": [
{
"feature": "high_amount_flag",
"impact": 0.2341,
"effect": "increase fraud risk"
},
{
"feature": "amount_deviation",
"impact": 0.1892,
"effect": "increase fraud risk"
},
...
]
}Decision Logic:
BLOCK: fraud_probability ≥ thresholdREVIEW: fraud_probability ≥ threshold × 0.7ALLOW: fraud_probability < threshold × 0.7
In a new terminal:
streamlit run dashboard.pyThe dashboard will open in your browser at http://localhost:8501
-
Transaction Input Form (Sidebar):
- Date and time picker
- Transaction amount
- Merchant category (10 options)
- Payment method (card, online, wallet)
- Country code (US, UK, LK, SG, DE)
- Transaction history features
- Behavioral flags (night, weekend, new merchant, geo jump, high amount)
-
Prediction Results:
- Fraud probability percentage
- Color-coded decision (ALLOW/REVIEW/BLOCK)
- Top 5 risk factors with SHAP values
- Interactive bar chart showing feature impacts
-
Demo Data:
- Sample recent transactions table
The model uses the following features:
Categorical:
merchant_category: groceries, electronics, travel, fuel, fashion, restaurants, health, subscriptions, gaming, luxurypayment_method: card, online, walletcountry_code: US, UK, LK, SG, DE
Numerical:
amount: Transaction amounthour: Hour of day (extracted from timestamp)txn_count_1h: Number of transactions in last hourtxn_count_24h: Number of transactions in last 24 hoursavg_amount_7d: Average transaction amount in last 7 daysamount_deviation: Absolute deviation from 7-day averagetime_since_last_txn: Minutes since last transaction
Binary Flags:
is_night: 1 if transaction between 22:00-06:00is_weekend: 1 if transaction on weekendnew_merchant_flag: 1 if first time with this merchantgeo_jump: 1 if country changed from previous transactionhigh_amount_flag: 1 if amount > 3× average
- Algorithm: XGBoost Classifier
- Objective: Binary logistic regression
- Evaluation Metric: AUC-PR (Area Under Precision-Recall Curve)
- Class Weighting: Automatic based on class imbalance
- Hyperparameters:
- n_estimators: 500
- max_depth: 6
- learning_rate: 0.05
- subsample: 0.8
- colsample_bytree: 0.8
The system uses SHAP TreeExplainer to provide:
- Feature importance scores
- Direction of impact (increases/reduces fraud risk)
- Top 5 risk drivers per prediction
curl -X POST "http://localhost:8000/predict" \
-H "Content-Type: application/json" \
-d '{
"timestamp": "2025-01-15T14:30:00",
"amount": 450.50,
"merchant_category": "electronics",
"payment_method": "card",
"country_code": "US",
"txn_count_1h": 2,
"txn_count_24h": 15,
"avg_amount_7d": 120.0,
"amount_deviation": 330.5,
"time_since_last_txn": 45.0,
"is_night": 0,
"is_weekend": 0,
"new_merchant_flag": 1,
"geo_jump": 0,
"high_amount_flag": 1
}'curl http://localhost:8000/health- The API expects categorical values that were seen during training. Unknown categories will return a 400 error.
- The model threshold is optimized for recall ≥ 0.8 to minimize false negatives (missed fraud).
- SHAP values are computed for the positive class (fraud) in binary classification.
- All timestamps should be in ISO format:
YYYY-MM-DDTHH:MM:SS
- Ensure you're running the API from the
api/directory, or - Check that all
.pklfiles are inapi/artifacts/
- Verify the API is running on
http://127.0.0.1:8000 - Check the
API_URLindashboard.pymatches your API address
- Ensure all dependencies are installed:
pip install -r requirements.txt - Activate your virtual environment if using one
This project is provided as-is for educational and demonstration purposes.
Feel free to submit issues, fork the repository, and create pull requests for any improvements.