-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathproject_credit_risk.py
More file actions
324 lines (243 loc) · 10.5 KB
/
project_credit_risk.py
File metadata and controls
324 lines (243 loc) · 10.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
# -*- coding: utf-8 -*-
"""Project_credit_risk.ipynb
Automatically generated by Colab.
Original file is located at
https://colab.research.google.com/drive/1Ck1UDi3-yUj2ilorPj1JROCll_SmIb9m
# Credit Risk Modeling using Logistic Regression (Supervised Learning)
## 1. Business Problem & Objective
In the lending industry, financial institutions need to decide whether a loan applicant is likely to default or not.
Incorrect decisions can lead to financial losses or missed business opportunities.
The objective of this project is to build a classification model that predicts the probability of loan default
using applicant-level financial and demographic information.
## 2. Dataset Overview
The dataset contains loan application records along with borrower details
and loan repayment status.
### Variables used in the dataset
**Personal and Financial Information**
- person_age: Age of the individual
- person_income: Annual income of the individual
- person_emp_length: Employment length in years
- person_home_ownership: Home ownership type (rent, mortgage, own, other)
**Loan Details**
- loan_amnt: Loan amount requested
- loan_int_rate: Interest rate of the loan
- loan_intent: Purpose of the loan
- loan_percent_income: Loan amount as a percentage of income
- loan_grade: Credit grade (A to G, where A is low risk and G is high risk)
**Credit History**
- cb_person_default_on_file: Previous default history (Y or N)
- cb_person_cred_hist_length: Length of credit history in years
**Target Variable**
- loan_status:
- 0: Non-default (loan repaid)
- 1: Default (loan not repaid)
"""
#IMPORT LIBRARIES
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
from sklearn.model_selection import train_test_split
from sklearn.preprocessing import StandardScaler
from sklearn.linear_model import LogisticRegression
from sklearn.metrics import confusion_matrix, classification_report, roc_auc_score, roc_curve
from imblearn.over_sampling import RandomOverSampler, SMOTE
"""## Let's have a look of the data"""
#BASIC DATA CHECK
df = pd.read_csv("credit_risk_dataset.csv")
df.head()
df.info()
"""## Exploratory Data Analysis (EDA)
EDA was performed to understand the distribution of the target variable.
Credit risk datasets are often imbalanced, which directly impacts model evaluation.
The class distribution shows the proportion of default and non-default loans.
"""
#TARGET VARIABLE ANALYSIS
df['loan_status'].value_counts()
df['loan_status'].value_counts(normalize=True)
"""* From the above summary we can observe that the dataset is imbalanced because the number of non-default cases is much
higher than default cases.
"""
df.describe()
"""## 4. Data Preprocessing
- Missing values were handled
- Categorical variables were encoded
- Features were scaled to ensure stable model convergence
- Data was split into training and test sets
"""
#MISSING VALUE CHECK
df.isnull().sum()
"""During exploratory data analysis, outliers were observed in some numerical
variables. Due to the presence of outliers, median imputation was used for
handling missing values, as it is more robust than the mean and is less
affected by extreme values.
"""
#HANDLE MISSING VALUES
df['person_emp_length'] = df['person_emp_length'].fillna(df['person_emp_length'].median())
df['loan_int_rate'] = df['loan_int_rate'].fillna(df['loan_int_rate'].median())
#HANDLE OUTLIERS
df['person_age'] = df['person_age'].clip(18, 75)
df['person_emp_length'] = df['person_emp_length'].clip(0, 40)
#ENCODE CATEGORICAL VARIABLES
cat_cols = [
'person_home_ownership',
'loan_intent',
'loan_grade',
'cb_person_default_on_file'
]
df = pd.get_dummies(df, columns=cat_cols, drop_first=True)
"""Categorical variables such as loan grade, loan purpose, home ownership, and
previous default status were converted into numerical form using dummy
variables so that they could be used in the machine learning model.
"""
#DEFINE DEPENDENT AND INDEPENDENT VARIABLES
X = df.drop('loan_status', axis=1)
y = df['loan_status']
#TRAIN–TEST SPLIT
X_train, X_test, y_train, y_test = train_test_split(
X, y,
test_size=0.3,
stratify=y,
random_state=42
)
#FEATURE SCALING
scaler = StandardScaler()
X_train_scaled = scaler.fit_transform(X_train)
X_test_scaled = scaler.transform(X_test)
"""### Handling Class Imbalance
The target variable is imbalanced, with fewer default cases compared to
non-default cases. To improve the model’s ability to identify defaulters,
resampling techniques were applied and compared with the baseline model.
## 5. Model Building (Baseline Logistic Regression)
Logistic Regression was used as a baseline model due to its interpretability
and widespread use in credit risk modeling.
### Model 1: Baseline Logistic Regression
This model is trained on the original dataset without applying any
resampling technique. It is used as a reference for comparison.
"""
model = LogisticRegression(max_iter=1000)
model.fit(X_train_scaled, y_train)
"""### MODEL EVALUATION
In credit risk modeling, Recall for the default class is particularly important,
as failing to identify defaulters can lead to direct financial losses.
"""
y_pred = model.predict(X_test_scaled)
y_prob = model.predict_proba(X_test_scaled)[:,1]
print("Confusion Matrix: \n",confusion_matrix(y_test, y_pred))
print("\nClassification Report:\n",classification_report(y_test, y_pred))
print("ROC-AUC:", roc_auc_score(y_test, y_prob))
"""### Model 2: Logistic Regression with Random Oversampling
Random oversampling increases recall for the minority class by duplicating
existing default observations in the training data.
"""
# Apply Random Oversampling
ros = RandomOverSampler(random_state=42)
X_ros, y_ros = ros.fit_resample(X_train, y_train)
# Scaling
scaler_ros = StandardScaler()
X_ros_scaled = scaler_ros.fit_transform(X_ros)
X_test_ros_scaled = scaler_ros.transform(X_test)
# Train model
model_ros = LogisticRegression(max_iter=1000)
model_ros.fit(X_ros_scaled, y_ros)
# Predictions
y_pred_ros = model_ros.predict(X_test_ros_scaled)
y_prob_ros = model_ros.predict_proba(X_test_ros_scaled)[:, 1]
"""### MODEL EVALUATION"""
print("Confusion Matrix: \n",confusion_matrix(y_test, y_pred_ros))
print("\nClassification Report:\n",classification_report(y_test, y_pred_ros))
print("ROC-AUC:", roc_auc_score(y_test, y_prob_ros))
"""### Model 3: Logistic Regression with SMOTE
SMOTE creates synthetic samples for the minority class, which helps the model
learn better decision boundaries compared to random duplication.
"""
# Apply SMOTE
smote = SMOTE(random_state=42)
X_smote, y_smote = smote.fit_resample(X_train, y_train)
# Scaling
scaler_smote = StandardScaler()
X_smote_scaled = scaler_smote.fit_transform(X_smote)
X_test_smote_scaled = scaler_smote.transform(X_test)
# Train model
model_smote = LogisticRegression(max_iter=1000)
model_smote.fit(X_smote_scaled, y_smote)
# Predictions
y_pred_smote = model_smote.predict(X_test_smote_scaled)
y_prob_smote = model_smote.predict_proba(X_test_smote_scaled)[:, 1]
"""### MODEL EVALUATION"""
print("Confusion Matrix: \n",confusion_matrix(y_test, y_pred_smote))
print("\nClassification Report:\n",classification_report(y_test, y_pred_smote))
print("ROC-AUC:", roc_auc_score(y_test, y_prob_smote))
"""## Model Comparison and Interpretation
Now we will compare the three models: a baseline logistic regression model, a model with
random oversampling, and a model with SMOTE, so that we can see how well each model identifies loan defaulters and how balanced the performance is for both default and non-default cases.
"""
from sklearn.metrics import (
precision_score,
recall_score,
precision_recall_fscore_support,
roc_auc_score
)
final_comparison = pd.DataFrame({
"Model": [
"Baseline Logistic Regression",
"Random Oversampling",
"SMOTE"
],
# Default class (1)
"Precision (Default=1)": [
precision_score(y_test, y_pred),
precision_score(y_test, y_pred_ros),
precision_score(y_test, y_pred_smote)
],
"Recall (Default=1)": [
recall_score(y_test, y_pred),
recall_score(y_test, y_pred_ros),
recall_score(y_test, y_pred_smote)
],
# Macro average (0 & 1 equally)
"Precision (Macro Avg)": [
precision_recall_fscore_support(y_test, y_pred, average="macro")[0],
precision_recall_fscore_support(y_test, y_pred_ros, average="macro")[0],
precision_recall_fscore_support(y_test, y_pred_smote, average="macro")[0]
],
"Recall (Macro Avg)": [
precision_recall_fscore_support(y_test, y_pred, average="macro")[1],
precision_recall_fscore_support(y_test, y_pred_ros, average="macro")[1],
precision_recall_fscore_support(y_test, y_pred_smote, average="macro")[1]
],
# ROC–AUC
"ROC-AUC": [
roc_auc_score(y_test, y_prob),
roc_auc_score(y_test, y_prob_ros),
roc_auc_score(y_test, y_prob_smote)
]
})
final_comparison
"""### Baseline Logistic Regression
The baseline model gives high precision for defaulters, which means that when
it predicts a default, the prediction is usually correct. However, the recall
for defaulters is low, showing that many actual defaulters are not identified.
Overall performance is good, but the model is conservative and misses risky
borrowers.
### Logistic Regression with Random Oversampling
The random oversampling model improves recall for defaulters, which means it
detects more high-risk borrowers. However, precision drops, leading to more
false alarms where non-defaulters are predicted as defaulters. The model covers
both classes well but is slightly aggressive.
### Logistic Regression with SMOTE
The SMOTE-based model shows a better balance between precision and recall for
defaulters. It performs more consistently across both default and non-default
classes. Although the ROC-AUC score is slightly lower than the other models,
the overall behavior of the model is more stable and balanced.
## 7. Business Interpretation of Results
- The model shows reasonable ability to distinguish between defaulters and non-defaulters.
- A higher recall for the default class indicates better risk detection.
- The model can be used as a decision-support tool alongside human judgment.
## 8. Conclusion
The baseline model is safe but misses many defaulters. Random oversampling
improves default detection but creates more false alarms. The SMOTE-based model
provides a good balance between identifying risky borrowers and maintaining
stable performance for all borrowers.
Considering default detection, balanced results, and practical use, the
SMOTE-based model is the most suitable choice for this credit risk assessment.
"""