๐ฏ Risk Alert Classifier
this project is to evaluate understanding of advanced supervised learning classification techniques, with a strong focus on imbalanced classification, resampling strategies (Under-Sampling, Over-Sampling, SMOTE, ADASYN), model generalization, hyperparameter tuning, and tree-based classification algorithms. Students will learn how to control overfitting, handle minority classes, and compare linear vs non-linear classifiers using real-world customer credit-risk data.
๐ Project Workflow
๐ Problem Statement
working on a bank's credit-risk analytics team. The company holds a customer risk dataset of 4,600 customers and wants a risk alert classifier that can predict whether a customer is risky (1) or safe (0) from their demographic, credit-behaviour and transaction attributes.
task to build a baseline classifier, evaluate it with confusion-matrix metrics, handle the class imbalance problem with resampling techniques, compare tree-based models, tune the best model with Randomized and Grid Search, and deliver a final recommendation that minimizes false negatives โ because missing a risky customer is the costliest mistake for the business.
๐ Live Streamlit App :-
๐ Project Files
| ๐ File / Folder |
๐ Description |
๐ Risk_Alert_Classifier.ipynb |
Main notebook โ the complete, annotated classification, imbalanced-learning, tree, tuning and ROC-analysis pipeline |
๐ Risk_Alert_Classifier_Dataset_4600 - Risk_Alert_Classifier_Dataset_4600.csv.csv |
Raw customer risk dataset (4,600 records) |
๐ README.md |
Project documentation and workflow guide |
๐ Visuals |
Folder of the output visuals/graphs |
๐ Part A :- Conceptual_Foundation.pdf |
Project documentation of part :- A (Theory) |
๐ Part H Final_Analysis_&_Reporting (Report).pdf |
Report documentation of part :- H (Report) |
๐ฅ๏ธ app.py |
Main Streamlit application for the live deployment |
โ๏ธ model.py.py |
Model file containing the trained prediction logic |
๐ฆ requirements.txt |
Required Python libraries and dependencies for the project |
๐ ๏ธ Tools Used
๐ฌ Project Demo
๐น Click on the badge to watch the video .
๐งฌ Dataset Structure โ Customer Risk Dataset
| Field Name |
Data Type |
Description |
Notes |
customer_id |
Integer |
Unique identifier for each customer |
Identifier only โ not a real feature |
age |
Float |
Age of the customer in years |
Has missing values โ KNN imputed |
gender |
Object |
Gender of the customer |
Categorical โ not used in the models |
region |
Object |
Region of the customer |
Categorical โ not used in the models |
employment_type |
Object |
Employment category (Salaried, Self-Employed, etc.) |
Categorical โ not used in the models |
annual_income_inr |
Float |
Annual income (โน) |
Model feature โ has missing values |
credit_score |
Float |
Customer's credit score |
Model feature โ has missing values |
credit_utilization_ratio |
Float |
Share of available credit being used |
Model feature โ has missing values |
missed_payments_12m |
Integer |
Missed payments in the last 12 months |
Model feature |
avg_late_payment_days |
Float |
Average days a payment is late |
Model feature |
monthly_transaction_count |
Integer |
Number of transactions per month |
Model feature |
monthly_spend_inr |
Float |
Monthly spending (โน) |
Model feature โ has missing values |
cash_advance_count_6m |
Integer |
Cash advances taken in the last 6 months |
Model feature |
complaints_last_6m |
Integer |
Complaints filed in the last 6 months |
Model feature |
failed_login_attempts_3m |
Integer |
Failed login attempts in the last 3 months |
Model feature |
account_tenure_months |
Integer |
How long the account has been open |
Model feature |
last_transaction_date |
Object (date) |
Date of the last transaction |
Text date โ not used as a model feature |
debt_balance_inr |
Integer |
Outstanding debt balance (โน) |
Model feature |
risk_status |
Integer |
1 = Risky, 0 = Safe |
๐ฏ Target variable (imbalanced: 12.11% risky) |
๐ง Part B : Dataset Understanding & Preparation
๐ Dataset Overview
df = pd.read_csv("Risk_Alert_Classifier_Dataset_4600 - Risk_Alert_Classifier_Dataset_4600.csv.csv")
df.head(); df.tail(); df.describe(); df.info(); df.shape
Output: 4,600 rows ร 19 columns ยท missing values in 7 columns (all under 5%)
๐ก Insight: credit_score (216), annual_income_inr (166), credit_utilization_ratio (147), employment_type (144), age (140), monthly_spend_inr (129) and region (102) all have missing values, but none exceeds 5% of the rows. customer_id is only an identifier and last_transaction_date is text, so neither can be used directly as a numeric feature. ๐
7๏ธโฃ Identify features and target variable
X = df.drop("risk_status", axis=1)
y = df["risk_status"]
Output: 18 input features ยท target = risk_status
๐ก Insight: The target is risk_status (1 = risky, 0 = safe) and every other column is a candidate feature. The target is binary, so this is a binary classification problem. customer_id sits in the feature list but carries no risk information, so it should be dropped before modelling. ๐ฏ
8๏ธโฃ Perform a train-test split while maintaining class distribution
X_train, X_test, y_train, y_test = train_test_split(
X, y, test_size=0.2, random_state=42, stratify=y
)
Output: X_train (3680, 18) ยท X_test (920, 18)
๐ก Insight: The 80/20 split gives 3,680 training and 920 testing rows, and stratify=y kept the class ratio almost identical โ risky is 12.12% in training and 12.07% in testing. The test set holds about 111 risky customers, so minority-class metrics rest on a small number of cases. ๐
9๏ธโฃ Identify missing values and apply KNN Imputer for multivariate imputation
imputer = KNNImputer(n_neighbors=5)
X_train[numeric_columns] = imputer.fit_transform(X_train[numeric_columns])
X_test[numeric_columns] = imputer.transform(X_test[numeric_columns])
Output: 0 missing values in all numeric columns after imputation
๐ก Insight: The imputer is fit on the training set only and then applied to the test set, so no test information leaks into training. It fills each missing value from the 5 most similar customers instead of a simple average, which preserves relationships between features. KNNImputer only works on numeric columns, so the missing values in employment_type and region are not filled by this step. โ๏ธ
๐ Part C : Baseline Classification Model
๐ Implement Logistic Regression as a baseline model
scaler = StandardScaler()
X_train_scaled = scaler.fit_transform(X_train[numeric_columns])
X_test_scaled = scaler.transform(X_test[numeric_columns])
model = LogisticRegression(random_state=42)
model.fit(X_train_scaled, y_train)
Output: Baseline Logistic Regression trained on 14 numeric features
๐ก Insight: The scaler is fit on the training set only, which prevents data leakage. Scaling matters because Logistic Regression is sensitive to feature scale. Only the numeric columns are used, so gender, region, employment_type and last_transaction_date are not part of the model. ๐
1๏ธโฃ1๏ธโฃ Generate and interpret the Confusion Matrix, Accuracy, Precision, Recall and F1-Score
cm = confusion_matrix(y_test, y_pred)
print("Accuracy:", accuracy_score(y_test, y_pred))
print("Precision:", precision_score(y_test, y_pred))
print("Recall:", recall_score(y_test, y_pred))
print("F1-Score:", f1_score(y_test, y_pred))
Output: Confusion Matrix [[809, 0], [0, 111]] ยท Accuracy 1.0 ยท Precision 1.0 ยท Recall 1.0 ยท F1-Score 1.0
๐ก Insight: All 809 safe and all 111 risky customers were classified correctly, so every metric is 1.0. A perfect score on 920 rows is unusual โ it suggests the numeric features separate the two classes almost linearly, but it also means this single split cannot show how the model behaves on harder cases. ๐ฏ
1๏ธโฃ2๏ธโฃ Identify Type-I and Type-II errors from the confusion matrix
TN, FP, FN, TP = cm.ravel()
print("Type-I Error (False Positive):", FP)
print("Type-II Error (False Negative):", FN)
Output: Type-I Error = 0 ยท Type-II Error = 0
๐ก Insight: A Type-I error (false positive) = 0 means no safe customer was wrongly flagged as risky, so no unnecessary alerts. A Type-II error (false negative) = 0 means no risky customer was missed โ in credit risk this is the costlier error, so this is the best possible result on this test set. โ๏ธ
โ๏ธ Part D : Handling Imbalanced Data
1๏ธโฃ3๏ธโฃ Demonstrate the impact of class imbalance on model performance
print(y.value_counts())
print(y.value_counts(normalize=True) * 100)
Output: Safe 4,043 (87.89%) ยท Risky 557 (12.11%)
๐ก Insight: The class distribution is about 7 safe customers for every risky one. A model that always predicted "safe" would reach roughly 88% accuracy while catching no risky customers, so accuracy alone is not a reliable measure here. In this notebook the imbalance did not hurt the baseline โ recall and F1 are both 1.0. ๐
1๏ธโฃ4๏ธโฃ Apply Under-Sampling, Over-Sampling, SMOTE and ADASYN, then retrain the model
under = RandomUnderSampler(random_state=42)
over = RandomOverSampler(random_state=42)
smote = SMOTE(random_state=42)
adasyn = ADASYN(random_state=42)
Output: Four rebalanced training sets, each used to retrain a Logistic Regression
๐ก Insight: All four techniques are applied only to the scaled training data โ the test set is untouched, so the comparison is fair. Under-sampling shrinks the safe class down to the risky class size and throws away safe-customer data; over-sampling duplicates risky rows; SMOTE and ADASYN create new synthetic risky rows instead. ๐
1๏ธโฃ5๏ธโฃ Compare performance before and after balancing (Recall, AUC-ROC, F1-Score)
| Model |
Recall |
F1-Score |
AUC-ROC |
| Before Balancing |
1.0 |
1.0000 |
1.000000 |
| Under-Sampling |
1.0 |
0.9823 |
1.000000 |
| Over-Sampling |
1.0 |
0.9867 |
1.000000 |
| SMOTE |
1.0 |
0.9911 |
1.000000 |
| ADASYN |
1.0 |
0.9694 |
0.999978 |
๐ก Insight: Recall is 1.0 for every method, and AUC-ROC is 1.0 for all except ADASYN. Since recall never dropped, the fall in F1 comes from lower precision โ roughly 4 false positives for Under-Sampling, 3 for Over-Sampling, 2 for SMOTE and 7 for ADASYN. SMOTE was the best of the four and ADASYN the worst. Balancing did not improve anything because the baseline was already perfect; it only added a few false alarms. โ๏ธ
๐ฒ Part E : Tree-Based Classification Models
1๏ธโฃ6๏ธโฃ Implement Decision Tree Classifier
dt_model = DecisionTreeClassifier(random_state=42)
dt_model.fit(X_train_scaled, y_train)
Output: Training Accuracy 1.0 ยท Testing Accuracy 0.9707
๐ก Insight: The unrestricted tree (no max_depth set) reaches 100% training accuracy โ it has memorized the training data. Its 97.07% test accuracy implies about 27 wrong predictions out of 920, whereas Logistic Regression made 0. ๐ฑ
1๏ธโฃ7๏ธโฃ Analyze overfitting by comparing training and testing performance