A full-stack web application for predicting bank credit risk using machine learning. This system uses a Decision Tree classifier to evaluate credit applications and provides detailed explanations for each prediction.
- Project Overview
- Features
- Architecture
- Tech Stack
- Installation
- Usage
- API Documentation
- Project Structure
- Model Details
- Deployment
- License
This application predicts whether a bank credit application will be classified as "good" (approved) or "bad" (rejected) based on various customer attributes. The system provides:
- Real-time credit risk prediction
- Visual analytics dashboard
- Prediction history tracking
- Explainable AI with feature importance
- Credit Risk Prediction: Predicts if a credit application is "good" or "bad"
- Probability Scoring: Provides confidence scores for each prediction
- Explainable Predictions: Generates feature-based explanations using decision tree paths
- Prediction History: Stores and retrieves past predictions
- Statistics Dashboard: Aggregates prediction data for analytics
- RESTful API: Full CRUD operations for predictions
- Interactive Dashboard: Visual analytics with multiple chart types
- Live Score Prediction: Real-time credit risk scoring as users fill the form
- Prediction Form: Comprehensive form for credit application data
- History View: Browse and filter past predictions
- Risk Distribution Charts: Pie charts showing risk distribution
- Amount & Duration Analysis: Distribution charts for credit parameters
- Radar Charts: Visual comparison of applicant attributes
┌─────────────────────────────────────────────────────────────┐
│ Frontend (React + Vite) │
│ ┌──────────┐ ┌──────────┐ ┌──────────┐ ┌──────────┐ │
│ │ Dashboard│ │ Form │ │ Charts │ │ History │ │
│ └──────────┘ └──────────┘ └──────────┘ └──────────┘ │
└─────────────────────────────────────────────────────────────┘
│
│ HTTP/JSON
▼
┌─────────────────────────────────────────────────────────────┐
│ Backend (Flask API) │
│ ┌──────────────┐ ┌──────────────┐ ┌──────────────┐ │
│ │ Prediction │ │ History │ │ Stats │ │
│ │ Endpoint │ │ Endpoint │ │ Endpoint │ │
│ └──────────────┘ └──────────────┘ └──────────────┘ │
└─────────────────────────────────────────────────────────────┘
│
▼
┌─────────────────────────────────────────────────────────────┐
│ ML Model (Decision Tree) │
│ ┌──────────────┐ ┌──────────────┐ ┌──────────────┐ │
│ │ Model │ │ Label │ │ Feature │ │
│ │ (.pkl) │ │ Encoders │ │ Processing │ │
│ └──────────────┘ └──────────────┘ └──────────────┘ │
└─────────────────────────────────────────────────────────────┘
- Python 3.x: Programming language
- Flask: Web framework
- Flask-CORS: Cross-origin resource sharing
- Scikit-learn: Machine learning library
- Joblib: Model serialization
- Pandas: Data manipulation
- NumPy: Numerical computations
- React 19: UI framework
- Vite 7: Build tool
- ESLint: Code linting
- Python 3.8+
- Node.js 18+
- npm or yarn
- Navigate to the backend directory:
bash
cd backend
- Create a virtual environment (optional but recommended):
bash
python -m venv venv
source venv/bin/activate # On Windows: venv\Scripts\activate
- Install Python dependencies:
bash
pip install flask flask-cors scikit-learn joblib pandas numpy gunicorn
- Start the Flask server locally:
bash
python app.py
The backend will run on http://localhost:5000
- Navigate to the frontend directory:
bash
cd front/BankPrediction
- Install dependencies:
bash
npm install
- Start the development server:
bash
npm run dev
The frontend will run on http://localhost:5173
- Start the backend server:
bash
cd backend && python app.py
- Start the frontend development server:
bash
cd front/BankPrediction && npm run dev
-
Open your browser and navigate to
http://localhost:5173 -
Fill in the credit application form with:
- Age
- Sex
- Job
- Housing
- Saving accounts
- Checking account
- Credit amount
- Duration
-
Click "Analyser" to get the prediction
http://localhost:5000
GET /health
Response:
json
{
"status": "healthy"
}
POST /predict
Request Body:
json
{
"age": 35,
"sex": "male",
"job": 2,
"housing": "own",
"saving_accounts": "little",
"checking_account": "little",
"credit_amount": 5000,
"duration": 24
}
Response:
json
{
"success": true,
"prediction": "good",
"probability": 0.85,
"risk_score": 0.15,
"explanation": [
{
"feature": "Duration",
"rule": "Duration <= 22.50",
"value": 24,
"impact": 0.45,
"signed_impact": -0.45,
"direction": "decreases_risk"
}
]
}
GET /history
GET /history/stats
DELETE /history/clear
GET /history/{id}
.
├── backend/
│ ├── app.py # Flask API application
│ ├── best_decision_tree_model.pkl # Trained model
│ ├── * _label_encoder.pkl # Label encoders for categorical features
│ ├── predictions_history.json # Prediction storage
│ └── test.py # Backend tests
│
├── front/
│ ├── BankPrediction/
│ │ ├── src/
│ │ │ ├── components/
│ │ │ │ ├── Header.jsx
│ │ │ │ ├── StatsCards.jsx
│ │ │ │ ├── MainChart.jsx
│ │ │ │ ├── RiskDistributionChart.jsx
│ │ │ │ ├── AmountDistributionChart.jsx
│ │ │ │ ├── RadarChart.jsx
│ │ │ │ ├── PredictionForm.jsx
│ │ │ │ ├── PredictionResult.jsx
│ │ │ │ ├── LiveScore.jsx
│ │ │ │ ├── History.jsx
│ │ │ │ └── index.js
│ │ │ ├── App.jsx
│ │ │ ├── App.css
│ │ │ ├── main.jsx
│ │ │ └── index.css
│ │ ├── package.json
│ │ ├── vite.config.js
│ │ └── index.html
│ │
│ ├── index.html # Vanilla JS version (deprecated)
│ └── script.js # Vanilla JS version (deprecated)
│
├── analysis_model.ipynb # Jupyter notebook for model analysis
├── fix_model.py # Model training/fixing script
├── german_credit_data.csv # Dataset
├── requirements.txt # Python dependencies (root level)
└── README.md # This file
- Source: German Credit Data
- Samples: 1000 credit applications
- Features: 8 input features + 1 target variable
| Feature | Type | Description |
|---|---|---|
| Age | Numeric | Customer age (18-100) |
| Sex | Categorical | male/female |
| Job | Numeric | Job type (0-3) |
| Housing | Categorical | own/rent/free |
| Saving accounts | Categorical | little/moderate/quite rich/rich |
| Checking account | Categorical | little/moderate/rich |
| Credit amount | Numeric | Loan amount in DM |
| Duration | Numeric | Loan duration in months |
- good: Credit approved
- bad: Credit rejected
- Install Vercel CLI:
bash
npm install -g vercel
- Navigate to the frontend directory:
bash
cd front/BankPrediction
- Login to Vercel:
bash
vercel login
- Deploy:
bash
vercel
- Set up environment variable for the API URL:
bash
vercel env add VITE_API_URL
Enter your Render backend URL (e.g., https://your-backend.onrender.com)
-
Push your code to a GitHub repository
-
Go to vercel.com and sign up
-
Click "New Project" and import your GitHub repository
-
Configure the project:
- Framework Preset:
Vite - Build Command:
npm run build - Output Directory:
dist
- Framework Preset:
-
Add environment variable:
- Name:
VITE_API_URL - Value: Your Render backend URL (e.g.,
https://bank-prediction-api.onrender.com)
- Name:
-
Click "Deploy"
Make sure to update the API URL in front/BankPrediction/src/App.jsx:
javascript
const API_URL = import.meta.env.VITE_API_URL || 'http://localhost:5000'
- Create a
requirements.txtfile in the backend directory:
text
flask
flask-cors
scikit-learn
joblib
pandas
numpy
gunicorn
- Create a
render.yamlfile in the backend directory (optional, for automatic configuration):
yaml
services:
- type: web
name: bank-prediction-api
env: python
buildCommand: pip install -r requirements.txt
startCommand: gunicorn app:app
-
Push your code to a GitHub repository
-
Go to render.com and sign up
-
Click "New" and select "Web Service"
-
Connect your GitHub repository
-
Configure the service:
- Name:
bank-prediction-api - Region: Choose closest to you
- Branch:
main - Runtime:
Python - Build Command:
pip install -r requirements.txt - Start Command:
gunicorn app:app
- Name:
-
Click "Deploy"
- Install Render CLI:
pip install render- Login:
bash
render login
- Navigate to backend directory:
bash
cd backend
- Create the service:
bash
render create service --name bank-prediction-api --type web --buildCommand "pip install -r requirements.txt" --startCommand "gunicorn app:app"
The backend is already configured with CORS support in backend/app.py:
from flask_cors import CORS
app = Flask(__name__)
CORS(app)For production, you may want to restrict CORS to only your Vercel domain:
python
CORS(app, origins=["https://your-vercel-project.vercel.app"])
Frontend (Vercel):
| Variable | Value |
|---|---|
| VITE_API_URL | https://your-backend.onrender.com |
Backend (Render): No special environment variables required (all config is in the code).
After deployment:
- Frontend:
https://your-project.vercel.app - Backend:
https://bank-prediction-api.onrender.com
Make sure to update the VITE_API_URL in Vercel to point to your Render backend URL.
This project is for educational purposes.
Created as a credit risk prediction demonstration using machine learning.