-
Notifications
You must be signed in to change notification settings - Fork 0
/
COLAB_PY_CODE
391 lines (241 loc) · 13.7 KB
/
COLAB_PY_CODE
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
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
# This project is aimed at predicting the case of customers' default payments in Taiwan.
# From the perspective of risk management, the result of predictive accuracy of the estimated probability of default will be more valuable
# than the binary result of classification - credible or not credible clients.
# We can use the K-S chart to evaluate which customers will default on their credit card payments.
from google.colab import drive
drive.mount('/content/drive')
import pandas as pd
import numpy as np
import warnings
warnings.filterwarnings("ignore")
import matplotlib.pyplot as plt
%matplotlib inline
import seaborn as sns
from sklearn.linear_model import LogisticRegression
from sklearn.ensemble import RandomForestClassifier
from xgboost import XGBClassifier
from sklearn.naive_bayes import GaussianNB
from sklearn.neighbors import KNeighborsClassifier
from imblearn.under_sampling import EditedNearestNeighbours
from sklearn.tree import DecisionTreeClassifier
from sklearn.svm import SVC
from sklearn.model_selection import train_test_split
from sklearn.model_selection import GridSearchCV
from sklearn.model_selection import StratifiedKFold
from sklearn.model_selection import RepeatedStratifiedKFold
from imblearn.over_sampling import SMOTE
from imblearn.combine import SMOTETomek
#from imblearn.under_sampling import SMOTE
from sklearn.compose import ColumnTransformer
from sklearn.preprocessing import PowerTransformer
from sklearn.metrics import classification_report, confusion_matrix, precision_recall_curve, auc, f1_score, make_scorer
from sklearn.metrics import accuracy_score, precision_score, recall_score, roc_auc_score #, plot_confusion_matrix
df1 = pd.read_csv("/content/drive/MyDrive/2 lab Nit mca/4th sem/INTERN/Credit Card Defaulter Prediction_csv.csv")
df1.head(5)
df1 = df1.rename(columns={"default ": "default_payment", "PAY_0": "PAY_1"})
df1_main = df1.copy()
df1_main.head(1)
df1.info()
# find null values
df1.isnull().sum()
# Renaming the target column for better reference. and changing PAY_0 to PAY_1.
df1.rename(columns={"default" : "default_payment", "PAY_0":"PAY_1"}, inplace=True)
df1.columns
df1["SEX"].value_counts()
df1.EDUCATION.value_counts()
df1.MARRIAGE.value_counts()
cat_columns = []
num_columns = []
for columns in df1.columns:
if df1[columns].nunique()<12:
cat_columns.append(columns)
else:
num_columns.append(columns)
print("Categorical Columns Are : ", cat_columns)
print("Numerical Columns Are : ", num_columns)
# Replace 'F' with 0 and 'M' with 1 in the 'SEX' column
df1['SEX'] = df1['SEX'].replace({'F': 0, 'MALE': 1})
df1.SEX.value_counts()
# Calculate the frequency of each category in the 'EDUCATION' column
education_counts = df1['EDUCATION'].value_counts()
# Sort the categories based on their frequencies in descending order
sorted_categories = education_counts.sort_values(ascending=False)
# Create a mapping dictionary to assign labels based on the sorted order
education_mapping = {category: i+1 for i, category in enumerate(sorted_categories.index)}
# Map the values in the 'EDUCATION' column using the mapping dictionary
df1['EDUCATION'] = df1['EDUCATION'].map(education_mapping)
fil = (df1['EDUCATION'] == 5) | (df1['EDUCATION'] == 6)
df1.loc[fil, 'EDUCATION'] = 4
df1['EDUCATION'].value_counts()
plot_vis(df1.EDUCATION)
df1['MARRIAGE'].value_counts()
# Calculate the frequency of each category in the 'EDUCATION' column
MARRIAGE_counts = df1['MARRIAGE'].value_counts()
# Sort the categories based on their frequencies in descending order
sorted_categories = MARRIAGE_counts.sort_values(ascending=False)
# Create a mapping dictionary to assign labels based on the sorted order
MARRIAGE_mapping = {category: i+1 for i, category in enumerate(sorted_categories.index)}
# Map the values in the 'EDUCATION' column using the mapping dictionary
df1['MARRIAGE'] = df1['MARRIAGE'].map(MARRIAGE_mapping)
fil = df1['MARRIAGE'] == 4
df1.loc[fil, 'MARRIAGE'] = 3
df1['MARRIAGE'].value_counts()
df1.MARRIAGE.value_counts()
import seaborn as sns
import matplotlib.pyplot as plt
# Set the style of seaborn
sns.set_style("whitegrid")
# Create a histogram for the 'AGE' column
plt.figure(figsize=(10, 6))
sns.histplot(data=df1, x='AGE', bins=30, kde=True, color='skyblue')
plt.title('Distribution of Age')
plt.xlabel('Age')
plt.ylabel('Frequency')
plt.show()
import matplotlib.pyplot as plt
import seaborn as sns
def plot_cat_columns(column_name):
# Clean column name (remove leading/trailing whitespaces)
column_name = column_name.strip()
plt.figure(figsize=(8, 6))
sns.countplot(data=df1, x=column_name, hue='default_payment')
plt.title(f'Defaults Distribution by {column_name}')
plt.xlabel(column_name)
plt.ylabel('Count')
plt.legend(title='Default Payment', loc='upper right')
plt.show()
plot_cat_columns("SEX")
# Replace 'Y' with 1 and 'N' with 0
df1['default_payment'] = df1['default_payment'].replace({'Y': 1, 'N': 0})
X = df1.drop(['default_payment'], axis=1)
y = df1['default_payment']
X_train, X_test, y_train, y_test = train_test_split(X,y, test_size=0.2, stratify=y, random_state=42)
!pip install scikit-learn
from sklearn.model_selection import cross_val_score
cv_scores = cross_val_score(LR, X, y, cv=5)
LR = LogisticRegression(random_state=0)
LR.fit(X_train, y_train)
y_pred = LR.predict(X_test)
print('Accuracy:', accuracy_score(y_pred,y_test))
cv_scores = cross_val_score(LR, X, y, cv=5)
print()
print(classification_report(y_test, y_pred))
print()
print("Average 5-Fold CV Score: {}".format(round(np.mean(cv_scores),4)),
", Standard deviation: {}".format(round(np.std(cv_scores),4)))
from sklearn.preprocessing import MinMaxScaler
scaler = MinMaxScaler()
for column in df1.columns:
if column != 'default_payment':
df1[column] = scaler.fit_transform(np.array(df1[column]).reshape(-1,1))
# Train Test Split
X = df1.drop(['default_payment'], axis=1)
y = df1['default_payment']
X_train, X_test, y_train, y_test = train_test_split(X,y, test_size=0.2, random_state=42)
# There's a lot of imbalance in the sample. So there are many ways to resampling! Resampling involves creating a new transformed version of the training dataset in which the selected examples have a different class distribution. This is a simple and effective strategy for imbalanced classification problems. The simplest strategy is to choose examples for the transformed dataset randomly, called random resampling. There are two main approaches to random resampling for imbalanced classification; they are oversampling and undersampling. -> Methods to solve class imbalance problem
# A) Random Oversampling: Randomly duplicate examples in the minority class. B) Random Undersampling: Randomly delete examples in the majority class. C) SMOTE: Synthetic Minority Oversampling Technique
# C) SMOTE: Synthetic Minority Oversampling Technique The upsample has the disadvantage of increasing the likelihood of overfitting since it replicates the minority class event. It usually outperform the downsampling. The downsample can discard potentially useful information and the sample can be biased, but it helps improving the run time SMOTE works by selecting examples that are close in the feature space, drawing a line between the examples in the feature space and drawing a new sample at a point along that line. To create a syntetic sample I want to use the SMOTE algorithm, which is an oversampling method which creates syntetic samples from the minority class instead of creating copies. It selects 2 or more similar instances and perturb them one at a time by random amount. This techniques should avoid overfitting problems but it risks adding noise to the model
sm = SMOTE(random_state=42)
X_SMOTE, y_SMOTE = sm.fit_resample(X_train, y_train)
print(y_SMOTE.value_counts())
# Helper Function for printing Accuracy matrices, plotting Confusiuon matrix and ROC curve
def helper(model,X_train,y_train,X_test,y_test):
train_pred = model.predict(X_train)
test_pred = model.predict(X_test)
train_acc = accuracy_score(train_pred, y_train)
test_acc = accuracy_score(test_pred, y_test)
prec = precision_score(y_test, test_pred)
recc = recall_score(y_test, test_pred)
f1 = f1_score(y_test, test_pred)
acc_matrices = {'Train accuracy':train_acc,'Test accuracy':test_acc,'Precision':prec,'Recall':recc,'F1 Score':f1}
print('\nTraining Accuracy Score: ',train_acc)
print('Testing Accuracy Score: ',test_acc)
print('Precision on test data: ',prec)
print('Recall on test data: ',recc)
print('F1 score on test data: ',f1)
print('\n========================================================')
print('Classification Report on Train data')
print(classification_report(train_pred, y_train))
print('\n========================================================')
print('Classification Report on Test data')
print(classification_report(test_pred, y_test))
print('\n========================================================')
# Plotting Confusion Matrix and ROC curve
f,ax = plt.subplots(1,2,figsize=(14,6))
#plt.figure(figsize=(6,4))
ConfMatrix = confusion_matrix(test_pred, y_test)
sns.heatmap(ConfMatrix,annot=True, cmap='YlGnBu', fmt="d",
xticklabels = ['Non-default', 'Default'],
yticklabels = ['Non-default', 'Default'],linewidths=.5,ax = ax[0])
ax[0].set_ylabel('True label')
ax[0].set_xlabel('Predicted label')
ax[0].set_title('Confusion Matrix')
global fpr,tpr,thresholds
fpr,tpr,thresholds = roc_curve(test_pred, y_test)
ax[1].plot(fpr,tpr,color = 'r')
ax[1].plot(fpr,fpr,color = 'green')
ax[1].set_ylabel('TPR')
ax[1].set_xlabel('FPR')
ax[1].set_title('ROC Curve')
plt.show()
return acc_matrices
%%time
lr = LogisticRegression()
lr = lr.fit(X_SMOTE, y_SMOTE)
Acc_mat_lr = helper(lr,X_SMOTE,y_SMOTE,X_test,y_test)
%%time
clf_tree = DecisionTreeClassifier( max_depth = 3, max_features=4, criterion='gini' )
clf_tree = clf_tree.fit(X_SMOTE, y_SMOTE)
Acc_mat_dt = helper(clf_tree,X_SMOTE,y_SMOTE,X_test,y_test)
%%time
from sklearn.ensemble import RandomForestClassifier
radm_clf = RandomForestClassifier(oob_score=True,n_estimators=100 , max_depth = 10, max_features=4, n_jobs=-1)
radm_clf = radm_clf.fit(X_SMOTE, y_SMOTE)
Acc_mat_radm = helper(radm_clf,X_SMOTE,y_SMOTE,X_test,y_test)
%%time
from sklearn.ensemble import AdaBoostClassifier
adb = AdaBoostClassifier(random_state=123)
adb = adb.fit(X_SMOTE, y_SMOTE)
Acc_mat_adb = helper(adb,X_SMOTE,y_SMOTE,X_test,y_test)
%%time
import xgboost as xgb
xgb = XGBClassifier(random_state=123)
xgb = xgb.fit(X_SMOTE, y_SMOTE)
Acc_mat_xgb = helper(xgb,X_SMOTE,y_SMOTE,X_test,y_test)
plot1=pd.DataFrame([Acc_mat_lr,Acc_mat_dt,Acc_mat_radm,Acc_mat_adb,Acc_mat_xgb], index=['Logistic Regression','DT','Random Forest','Adaboost','Xgboost']).transpose()
plot1.plot(kind='bar',figsize=(8,6))
plt.xlabel("Accuracy Matrices",fontsize=15)
plt.ylabel("Accuracy",fontsize=15)
plt.show()
# XG BOOST IS GIVING BEST ACCURACY
# To create a Kolmogorov-Smirnov (K-S) chart to evaluate which customers will default on their credit card payments, we typically use the predicted probabilities of default generated by a model and the actual outcomes (i.e., whether the customers defaulted or not). Below is a general outline of how to create a K-S chart using Python:
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
# Assuming you have the DataFrame with the preprocessed and scaled data
# Let's say you have it in a DataFrame named 'df'
# Sort the DataFrame by predicted probabilities if available, or use 'default_payment' column directly
df = df.sort_values(by='default_payment', ascending=False)
# Calculate cumulative proportions of default and non-default cases
df['Cumulative_Default'] = np.cumsum(df['default_payment'])
df['Cumulative_Non_Default'] = np.cumsum(1 - df['default_payment'])
# Calculate cumulative proportions of total cases
df['Total_Cumulative'] = np.arange(1, len(df) + 1) / len(df)
# Calculate KS statistic: max difference between cumulative default and cumulative non-default proportions
ks_statistic = (df['Cumulative_Default'] - df['Cumulative_Non_Default']).abs().max()
# Plot K-S chart
plt.plot(df['Total_Cumulative'], df['Cumulative_Default'], label='Cumulative Default')
plt.plot(df['Total_Cumulative'], df['Cumulative_Non_Default'], label='Cumulative Non-Default')
plt.fill_between(df['Total_Cumulative'], df['Cumulative_Default'], df['Cumulative_Non_Default'], color='grey', alpha=0.2)
plt.title(f'K-S Chart (KS Statistic: {ks_statistic:.4f})')
plt.xlabel('Total Cumulative Proportion')
plt.ylabel('Cumulative Proportion')
plt.legend()
plt.grid(True)
plt.show()
# The Kolmogorov-Smirnov (K-S) chart visually represents the ability of a model to discriminate between different classes, in this case, between customers who defaulted on their credit card payments and those who did not.
# Interpreting the K-S chart involves examining the maximum vertical distance (KS statistic) between the cumulative proportion of default cases and the cumulative proportion of non-default cases.
# If the KS statistic is close to 1, it indicates that the model has strong discriminatory power, meaning it effectively separates defaulters from non-defaulters.
# A lower KS statistic suggests weaker discriminatory power, implying that the model may struggle to differentiate between default and non-default cases.
# Additionally, analyzing the K-S chart can help identify the optimal threshold for classification. The point on the curve where the KS statistic is maximum corresponds to the threshold that maximizes the model's ability to discriminate between the classes.
# Overall, a K-S chart provides valuable insights into the performance of a classification model and aids in evaluating its predictive accuracy and discriminatory power.