-
Notifications
You must be signed in to change notification settings - Fork 1
/
Credit Risk Analysis codes.py
663 lines (564 loc) · 18.1 KB
/
Credit Risk Analysis codes.py
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
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
#!/usr/bin/env python
# coding: utf-8
# # Credit Risk Analysis
# # *Importing Libraries*
# In[1]:
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import seaborn as sns
##### Importing Dataset from Text File
Credit_DS = pd.read_csv(r"C:\Users\Sanyu\Imarticus\Projects\Python Project\Credit_Risk_Analysis.txt", delimiter="\t", header=0, engine='python')
print(Credit_DS)
#%%
Credit_DS.shape
Credit_DS.head()
print(Credit_DS.dtypes)
Credit_DS.describe()
### Creating A Copy of DataFrame
Credit_DS_Backup = Credit_DS.copy()
Credit_DS_Backup.shape
print(Credit_DS_Backup)
#%%
##### Pre Processing the Data
### Find Missing Value and Calculating Missing Value Percentage.
#%%
Count_Null=Credit_DS.isnull().sum()
print(Count_Null)
#%%
Per_Null =round(Credit_DS.isnull().sum()/len(Credit_DS) * 100,2)
print(Per_Null)
#%%
Missing_Val= pd.DataFrame({'count':Count_Null,'% Count':Per_Null})
print(Missing_Val)
#%%
Missing_Val.sort_values(by=['% Count'],ascending=False)
#%%
Missing_Val.to_csv(r'D:\Desktop\MissingValues.csv')
#%%
### Deleting Variable having Missing Values More than 50%
#%%
Credit_DS = Credit_DS.loc[:,Credit_DS.isnull().sum()/len(Credit_DS) <.50 ]
#%%
Credit_DS.shape
#%%
Credit_DS.to_csv(r'D:\Desktop\Rem_Data.csv')
#%%
### Deleting Variables based on Domain Knowledge
Credit_DS=Credit_DS.drop(['id','member_id','sub_grade','emp_title','title','zip_code','addr_state','earliest_cr_line','inq_last_6mths',
'last_pymnt_d','next_pymnt_d','last_credit_pull_d','pymnt_plan','policy_code'],axis=1)
#%%
Credit_DS.shape
#%%
Credit_DS.to_csv(r'D:\Desktop\Rem_FinalData.csv')
#%%
Credit_DS.isnull().sum()
#%%
##### Assigning Label to Categorical Data and Converting Numerical
### Term Column Labelling
#%%
Credit_DS.term.value_counts()
#%%
Credit_DS.term.value_counts()
Credit_DS.term=Credit_DS.term.str.extract('(\d+)')
term_final={'36':0,'60':1}
Credit_DS.term=[term_final[item]for item in Credit_DS.term]
print(Credit_DS.term)
# 36 Months - 0
# 60 Months - 1
#%%
### Grade Column Labelling
Credit_DS.grade.value_counts()
#%%
Credit_DS.grade.value_counts()
grade_final={'A':6,'B':5,'C':4,'D':3,'E':2,'F':1,'G':0}
Credit_DS.grade=[grade_final[item]for item in Credit_DS.grade]
print(Credit_DS.grade)
#%%
### Emp_Length Columns Labeling
A = Credit_DS.emp_length.isnull().sum()
A
#%%
Credit_DS.emp_length.value_counts()
#%%
Credit_DS['emp_length'].mode()[0]
Credit_DS['emp_length'].fillna(Credit_DS['emp_length'].mode()[0],inplace=True)
Credit_DS['emp_length'] = Credit_DS['emp_length'].str.replace('+','')
Credit_DS['emp_length'] = Credit_DS['emp_length'].str.replace('<','')
print(Credit_DS.emp_length)
Credit_DS.emp_length=Credit_DS.emp_length.str.extract('(\d+)')
print(Credit_DS.emp_length)
Credit_DS.emp_length = [int(x) for x in Credit_DS.emp_length]
print(Credit_DS.emp_length.dtype)
#%%
###Home_Ownership labeling
#%%
Credit_DS.home_ownership.value_counts()
#%%
print(Credit_DS['home_ownership'].unique())
home_ownership_score={'NONE':0,'ANY':0,'OTHER':0,'RENT':1,'MORTGAGE':2,'OWN':3}
Credit_DS.home_ownership=[home_ownership_score[item]for item in Credit_DS.home_ownership]
print(Credit_DS.home_ownership)
#%%
### Verification_statusColumns Labeling
#%%
Credit_DS.verification_status.value_counts()
#%%
print(Credit_DS['verification_status'].unique())
Ver_status={'Verified':1,'Source Verified':1,'Not Verified':0}
Credit_DS.verification_status=[Ver_status[item]for item in Credit_DS.verification_status]
print(Credit_DS.verification_status)
#%%
### Purpose Plan Columns Labeling
#%%
Credit_DS.purpose.value_counts()
#%%
print(Credit_DS['purpose'].unique())
purpose_score={'vacation':0,'other':1,'major_purchase':2,'car':3,'wedding':4,'medical':5,'moving':6,'home_improvement':7,'credit_card':8,'debt_consolidation':9,'house':10,'small_business':11,'renewable_energy':12,'educational':13}
Credit_DS.purpose=[purpose_score[item] for item in Credit_DS.purpose]
print(Credit_DS.purpose)
#%%
### Treament of Missing value of Revol_util Columns
Credit_DS.revol_util.describe()
print(Credit_DS.revol_util.isnull().sum())
Credit_DS['revol_util'].fillna(Credit_DS['revol_util'].mean(),inplace=True)
print(Credit_DS.revol_util.isnull().sum())
#%%
### Initial List Status Columns Labeling
#%%
Credit_DS.initial_list_status.value_counts()
#%%
print(Credit_DS['initial_list_status'].unique())
list_status={'w':1,'f':2}
Credit_DS.initial_list_status=[list_status[item] for item in Credit_DS.initial_list_status]
print(Credit_DS.initial_list_status)
#%%
### ApplicationType Columns Labeling
#%%
Credit_DS.application_type.value_counts()
#%%
print(Credit_DS['application_type'].unique())
App_type={'INDIVIDUAL':1,'JOINT':2}
Credit_DS.application_type=[App_type[item] for item in Credit_DS.application_type]
print(Credit_DS.application_type)
#%%
### Open Account Labeling
#%%
Credit_DS.open_acc.value_counts()
#%%
Credit_DS.open_acc.mode()
def open_acc_final(i):
if i in range (4,25):
return(1)
else:
return(0)
Credit_DS['open_acc']= Credit_DS['open_acc'].apply(open_acc_final)
print(Credit_DS.open_acc)
#%%
### Treating Missing Value for collections_12_mths_ex_med
Credit_DS.collections_12_mths_ex_med.value_counts()
#%%
Credit_DS.collections_12_mths_ex_med.describe()
print(Credit_DS.collections_12_mths_ex_med.isnull().sum())
Credit_DS['collections_12_mths_ex_med'].fillna(Credit_DS['collections_12_mths_ex_med'].median(),inplace=True)
print(Credit_DS.collections_12_mths_ex_med.isnull().sum())
#%%
### Treating Missing Value for tot_coll_amt
#%%
Credit_DS.tot_coll_amt.describe()
print(Credit_DS.tot_coll_amt.isnull().sum())
Credit_DS['tot_coll_amt'].fillna(Credit_DS['tot_coll_amt'].mean(),inplace=True)
print(Credit_DS.tot_coll_amt.isnull().sum())
#%%
### Treating Missing Value for tot_cur_bal
Credit_DS.tot_cur_bal.describe()
print(Credit_DS.tot_cur_bal.isnull().sum())
Credit_DS['tot_cur_bal'].fillna(Credit_DS['tot_cur_bal'].mean(),inplace=True)
print(Credit_DS.tot_cur_bal.isnull().sum())
#%%
### Treating Missing Value for total_rev_hi_lim
#%%
Credit_DS.total_rev_hi_lim.describe()
print(Credit_DS.total_rev_hi_lim.isnull().sum())
Credit_DS['total_rev_hi_lim'].fillna(Credit_DS['total_rev_hi_lim'].mean(),inplace=True)
print(Credit_DS.total_rev_hi_lim.isnull().sum())
#%%
import warnings
warnings.filterwarnings("ignore")
import matplotlib.pyplot as plt
import numpy as np
import pandas as pd
import seaborn as sns
import sklearn
from statistics import mode
from sklearn.preprocessing import StandardScaler
from sklearn.model_selection import train_test_split, cross_val_score, cross_val_predict
from sklearn.preprocessing import Imputer, RobustScaler, FunctionTransformer
from sklearn.ensemble import RandomForestClassifier, VotingClassifier, GradientBoostingClassifier
from sklearn.ensemble.partial_dependence import plot_partial_dependence
from sklearn.linear_model import LogisticRegression
from sklearn.svm import SVC, LinearSVC
from sklearn.neighbors import KNeighborsClassifier
from sklearn.metrics import (roc_auc_score, confusion_matrix,
accuracy_score, roc_curve,
precision_recall_curve, f1_score)
from sklearn.pipeline import make_pipeline
from scipy.stats import boxcox
from sklearn.metrics import confusion_matrix
get_ipython().run_line_magic('matplotlib', 'inline')
plt.style.use("fivethirtyeight")
sns.set_context("notebook")
#%%
### Heat Map for Multi Colinearity
#%%
get_ipython().run_line_magic('matplotlib', 'inline')
plt.style.use("fivethirtyeight")
sns.set_context("notebook")
plt.figure(figsize=(20,20))
sns.set_context("paper", font_scale=1.2)
#%%
sns.heatmap(Credit_DS.assign(grade=Credit_DS.grade.astype('category').cat.codes,
term=Credit_DS.term.astype('category').cat.codes,
verification=Credit_DS.verification_status.astype('category').cat.codes,
emp_length=Credit_DS.emp_length.astype('category').cat.codes,
home=Credit_DS.home_ownership.astype('category').cat.codes,
purpose=Credit_DS.purpose.astype('category').cat.codes,
list_status=Credit_DS.initial_list_status.astype('category').cat.codes,
application=Credit_DS.application_type.astype('category').cat.codes,
openacc=Credit_DS.open_acc.astype('category').cat.codes).corr(),
annot=False, cmap='PuBuGn', vmin=-1, vmax=1, square=True, linewidths=0.5)
plt.show()
#%%
### Split Train and Test data with issue_d date.
#%%
Credit_DS.issue_d=pd.to_datetime(Credit_DS.issue_d,infer_datetime_format=True)
col_name='issue_d'
print(Credit_DS[col_name].dtype)
#%%
split_data="2015-06-01"
Training=Credit_DS[Credit_DS['issue_d']<split_data]
Training.shape
#%%
Testing=Credit_DS.loc[Credit_DS['issue_d']>='2015-06-01',:]
Testing.shape
#%%
Training=Training.drop(['issue_d'],axis=1)
Testing=Testing.drop(['issue_d'],axis=1)
#%%
X_train = Training.values[:,:-1]
Y_train = Training.values[:,-1]
#%%
X_test = Testing.values[:,:-1]
Y_test = Testing.values[:,-1]
### Scaling The Data
#%%
from sklearn.preprocessing import StandardScaler
scaler= StandardScaler()
scaler.fit(X_train)
X_train=scaler.transform(X_train)
X_test=scaler.transform(X_test)
print(X_train)
print(X_test)
#%%
Y_train=Y_train.astype(int)
Y_test=Y_test.astype(int)
#%%
### Logistic Regression MODEL BUILDING
#%%
from sklearn.linear_model import LogisticRegression
classifier= LogisticRegression()
classifier.fit(X_train,Y_train)
Y_pred = classifier.predict(X_test)
print(list(zip(Y_test,Y_pred)))
#%%
print(classifier.coef_)
print(classifier.intercept_)
#%%
### Model Evaluation
#%%
from sklearn.metrics import confusion_matrix, accuracy_score,classification_report
cfm = confusion_matrix(Y_test, Y_pred)
print(cfm)
print("Classification Report :")
print(classification_report(Y_test, Y_pred))
acc= accuracy_score(Y_test, Y_pred)
print("Accuracy of the model: ",acc)
#%%
fig, axes = plt.subplots(figsize=(8,6))
cm = confusion_matrix(Y_test, Y_pred)
cm = (cm.astype('float')/cm.sum(axis=0))*100
ax=sns.heatmap(cm,annot=True,cmap='magma');
ax.set_xlabel("Actual Values")
ax.set_ylabel("Our Predicted Values")
ax.axis('equal')
#%%
y_pred_prob= classifier.predict_proba(X_test)
print(y_pred_prob)
#%%
##TO FIND WHICH THRESHOLD IS BETTER
for a in np.arange(0,1,0.01):
predict_mine = np.where(y_pred_prob[:,1] > a, 1, 0)
cfm=confusion_matrix(Y_test, predict_mine)
total_err=cfm[0,1]+cfm[1,0]
print("Errors at threshold ", a, ":",total_err, " , type 2 error :",
cfm[1,0]," , type 1 error:", cfm[0,1])
#%%
from sklearn import metrics
fpr , tpr , z = metrics.roc_curve(Y_test , y_pred_prob[:,1])
auc = metrics.auc(fpr,tpr)
print(auc)
print(fpr)
print(tpr)
#%%
#ROC curve
import matplotlib.pyplot as plt
#%matplotlib inline
plt.title('Receiver Operating Characteristic')
plt.plot(fpr, tpr, 'b', label = auc)
plt.legend(loc = 'lower right')
plt.plot([0, 1], [0, 1],'r--')
plt.xlim([0, 1])
plt.ylim([0, 1])
plt.xlabel('False Positive Rate')
plt.ylabel('True Positive Rate')
plt.show()
#%%
##K - FOLD METHOD FOR CROSS VALIDATION
#Using cross validation
classifier=(LogisticRegression())
#performing kfold_cross_validation
from sklearn.model_selection import KFold
kfold_cv=KFold(n_splits=10)
print(kfold_cv)
#%%
from sklearn.model_selection import cross_val_score
#running the model using scoring metric as accuracy
kfold_cv_result=cross_val_score(estimator=classifier,X=X_train,
y=Y_train, cv=kfold_cv)
print(kfold_cv_result)
#finding the mean
print(kfold_cv_result.mean())
#Gives multiple accuracies and finally gives mean of all accuracies
#82.40% is approx close to 85% , hence we go ahead with base model
#if we want to go ahead with kfold model then execute the next block
#%%
for train_value, test_value in kfold_cv.split(X_train):
classifier.fit(X_train[train_value], Y_train[train_value]).predict(X_train[test_value])
#%%
Y_pred=classifier.predict(X_test)
#%%
from sklearn.metrics import confusion_matrix, accuracy_score,classification_report
cfm = confusion_matrix(Y_test, Y_pred)
print(cfm)
print("Classification report: ")
#recall gives accuracy for individual value(sensitivity for 0(TNR),specificity for 1(TPR))
#precision gives precision of class 0 and class 1 (how many predictions were correct?)
print(classification_report(Y_test, Y_pred))
acc= accuracy_score(Y_test, Y_pred)
print("Accuracy of the model: ",acc)
#%%
### Decision Tree Model
#%%
from sklearn.tree import DecisionTreeClassifier
model_DT = DecisionTreeClassifier(random_state=10,min_samples_leaf=100,max_depth=25,criterion='gini')
#default criterion is gini
model_DT.fit(X_train,Y_train)
#%%
Y_pred = model_DT.predict(X_test)
#print(Y_pred)
print(list(zip(Y_test,Y_pred)))
#%%
### Evaluation of Model
from sklearn.metrics import confusion_matrix, accuracy_score,classification_report
cfm = confusion_matrix(Y_test, Y_pred)
print(cfm)
print("Classification report: ")
print(classification_report(Y_test, Y_pred))
acc= accuracy_score(Y_test, Y_pred)
print("Accuracy of the model: ",acc)
#%%
fig, axes = plt.subplots(figsize=(8,6))
cm = confusion_matrix(Y_test, Y_pred)
cm = (cm.astype('float')/cm.sum(axis=0))*100
ax=sns.heatmap(cm,annot=True,cmap='magma');
ax.set_xlabel("Actual Values")
ax.set_ylabel("Our Predicted Values")
ax.axis('equal')
#%%
colname=Training.columns[:]
colname
#%%
from sklearn import tree
with open("model_DecisionTree.txt","w") as f:
f = tree.export_graphviz(model_DT,feature_names=colname[:-1],out_file=f)
#%%
from sklearn import metrics
fpr , tpr , z = metrics.roc_curve(Y_test , Y_pred)
auc = metrics.auc(fpr,tpr)
print(auc)
print(fpr)
print(tpr)
#%%
#ROC curve
import matplotlib.pyplot as plt
#%matplotlib inline
plt.title('Receiver Operating Characteristic')
plt.plot(fpr, tpr, 'b', label = auc)
plt.legend(loc = 'lower right')
plt.plot([0, 1], [0, 1],'r--')
plt.xlim([0, 1])
plt.ylim([0, 1])
plt.xlabel('False Positive Rate')
plt.ylabel('True Positive Rate')
plt.show()
#%%
### ENSEMBLE MODEL
from sklearn.linear_model import LogisticRegression
from sklearn.tree import DecisionTreeClassifier
from sklearn.ensemble import VotingClassifier
#%%
estimators = []
model1 = LogisticRegression()
estimators.append(('log', model1))
model2 = DecisionTreeClassifier()
estimators.append(('cart', model2))
model3 = SVC(kernel='rbf',gamma=0.1,C=70.0)
estimators.append(('svm', model3))
print(estimators)
#%%
ensemble = VotingClassifier(estimators)
ensemble.fit(X_train,Y_train)
Y_pred=ensemble.predict(X_test)
#%%
### Gradient Boosting Classifier
#%%
from sklearn.ensemble import GradientBoostingClassifier
model_GradientBoosting=GradientBoostingClassifier()
#model_GradientBoosting=DecisionTreeClassifier()
#fit the model on the data and predict the values
model_GradientBoosting.fit(X_train,Y_train)
Y_pred=model_GradientBoosting.predict(X_test)
#checking result
from sklearn.metrics import confusion_matrix, accuracy_score,classification_report
#confusion_matrix
cfm=confusion_matrix(Y_test,Y_pred)
print(cfm)
#classification_report
print("Classification report: ")
print(classification_report(Y_test,Y_pred))
#accuracy_score
acc=accuracy_score(Y_test, Y_pred)
print("Accuracy of the model: ",acc)
#%%
fig, axes = plt.subplots(figsize=(8,6))
cm = confusion_matrix(Y_test, Y_pred)
cm = cm.astype('float')/cm.sum(axis=0)
ax=sns.heatmap(cm,annot=True,cmap='magma');
ax.set_xlabel("Actual Values")
ax.set_ylabel("Our Predicted Values")
ax.axis('equal')
#%%
from sklearn import metrics
fpr , tpr , z = metrics.roc_curve(Y_test , Y_pred)
auc = metrics.auc(fpr,tpr)
print(auc)
print(fpr)
print(tpr)
#%%
#ROC curve
import matplotlib.pyplot as plt
#%matplotlib inline
plt.title('Receiver Operating Characteristic')
plt.plot(fpr, tpr, 'b', label = auc)
plt.legend(loc = 'lower right')
plt.plot([0, 1], [0, 1],'r--')
plt.xlim([0, 1])
plt.ylim([0, 1])
plt.xlabel('False Positive Rate')
plt.ylabel('True Positive Rate')
plt.show()
#%%
### ADA Booster Classifier
#%%
from sklearn.ensemble import AdaBoostClassifier
ada_model=AdaBoostClassifier()
ada_model.fit(X_train,Y_train)
Y_pred=ada_model.predict(X_test)
from sklearn.metrics import confusion_matrix, accuracy_score,classification_report
cfm=confusion_matrix(Y_test,Y_pred)
print(cfm)
print("Classification report: ")
print(classification_report(Y_test,Y_pred))
acc=accuracy_score(Y_test, Y_pred)
print("Accuracy of the model: ",acc)
#%%
fig, axes = plt.subplots(figsize=(8,6))
cm = confusion_matrix(Y_test, Y_pred)
cm = (cm.astype('float')/cm.sum(axis=0))*100
ax=sns.heatmap(cm,annot=True,cmap='magma');
ax.set_xlabel("Actual Values")
ax.set_ylabel("Our Predicted Values")
ax.axis('equal')
#%%
from sklearn import metrics
fpr , tpr , z = metrics.roc_curve(Y_test , Y_pred)
auc = metrics.auc(fpr,tpr)
print(auc)
print(fpr)
print(tpr)
#%%
#ROC curve
import matplotlib.pyplot as plt
#%matplotlib inline
plt.title('Receiver Operating Characteristic')
plt.plot(fpr, tpr, 'b', label = auc)
plt.legend(loc = 'lower right')
plt.plot([0, 1], [0, 1],'r--')
plt.xlim([0, 1])
plt.ylim([0, 1])
plt.xlabel('False Positive Rate')
plt.ylabel('True Positive Rate')
plt.show()
#%%
### Extra Tree Classifier
#%%
from sklearn.ensemble import ExtraTreesClassifier
model=(ExtraTreesClassifier(4,random_state=100))
model=model.fit(X_train,Y_train)
Y_pred=model.predict(X_test)
from sklearn.metrics import confusion_matrix, accuracy_score,classification_report
cfm=confusion_matrix(Y_test,Y_pred)
print(cfm)
print("Classification report: ")
print(classification_report(Y_test,Y_pred))
acc=accuracy_score(Y_test, Y_pred)
print("Accuracy of the model: ",acc)
#%%
fig, axes = plt.subplots(figsize=(8,6))
cm = confusion_matrix(Y_test, Y_pred)
cm = (cm.astype('float')/cm.sum(axis=0))*100
ax=sns.heatmap(cm,annot=True,cmap='magma');
ax.set_xlabel("Actual Values")
ax.set_ylabel("Our Predicted Values")
ax.axis('equal')
#%%
from sklearn import metrics
fpr , tpr , z = metrics.roc_curve(Y_test , Y_pred)
auc = metrics.auc(fpr,tpr)
print(auc)
print(fpr)
print(tpr)
#%%
#ROC curve
import matplotlib.pyplot as plt
#%matplotlib inline
plt.title('Receiver Operating Characteristic')
plt.plot(fpr, tpr, 'b', label = auc)
plt.legend(loc = 'lower right')
plt.plot([0, 1], [0, 1],'r--')
plt.xlim([0, 1])
plt.ylim([0, 1])
plt.xlabel('False Positive Rate')
plt.ylabel('True Positive Rate')
plt.show()
#%%