-
Notifications
You must be signed in to change notification settings - Fork 2
/
functions.py
858 lines (662 loc) · 35 KB
/
functions.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
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
# Import base libraries
import pandas as pd
import numpy as np
from scipy.io import arff
import matplotlib.pyplot as plt
import seaborn as sns
from sklearn.model_selection import train_test_split
from sklearn.preprocessing import StandardScaler
from xgboost import XGBClassifier
from sklearn.metrics import roc_auc_score, roc_curve, auc
from sklearn.metrics import precision_score, recall_score, accuracy_score, f1_score
from sklearn.utils import class_weight
from sklearn.metrics import classification_report
def xgb_model_report(dataNumber, X_tr, y_tr, X_te, y_te, xgbParams, model_name, weights=0, save_model=0, print_report=0):
"""
This is a function to that runs XGBClassifier with the given parameters and print the classification report.
Returns to the model.
dataNumber: # for Data file in use (1, 2, 3, 4, 5)
X_tr: training data
y_tr: training labels
X_te: testing data
y_te: testinglabels
xgbParams: XGBClassifier parameters used to create the model
weights: Bool parameter to use sample_weights or not.
save_model: Bool parameter to control saving the model
print_report: Bool parameter to control printing the report
"""
weigths_train = None
if weights:
print('Sample weights are used!')
weigths_train = class_weight.compute_sample_weight(class_weight='balanced', y=y_tr)
d_eval_set = [(X_tr, y_tr), (X_te, y_te)]
clf = XGBClassifier(**xgbParams)
clf.fit(X_tr, y_tr, sample_weight=weigths_train, eval_set=d_eval_set, verbose=False)
if print_report:
print(f'Data {dataNumber} Classification Report:\n')
print('Training Data:\n', classification_report(y_tr, clf.predict(X_tr)))
print('Testing Data:\n', classification_report(y_te, clf.predict(X_te)))
if save_model:
clf.save_model(f'saved_model_history/xgb_data{dataNumber}_{model_name}.json')
return clf
def xgb_model_report2(dataNumber, df, xgbParams, model_name, weights=0, save_model=0, print_report=0):
"""
This is a function to that runs XGBClassifier with the given parameters and print the classification report.
Returns to the model.
dataNumber: # for Data file in use (1, 2, 3, 4, 5)
df: Dataframe
xgbParams: XGBClassifier parameters used to create the model
weights: Bool parameter to use sample_weights or not.
save_model: Bool parameter to control saving the model
print_report: Bool parameter to control printing the report
"""
#Pre-process data
y = df['class']
X = df.drop('class', axis=1)
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.20, random_state=42)
scaler = StandardScaler()
X_tr = scaler.fit_transform(X_train)
X_te = scaler.transform(X_test)
y_tr = y_train.to_numpy()
y_te = y_test.to_numpy()
weigths_train = None
if weights:
print('Sample weights are used!')
weigths_train = class_weight.compute_sample_weight(class_weight='balanced', y=y_tr)
d_eval_set = [(X_tr, y_tr), (X_te, y_te)]
clf = XGBClassifier(**xgbParams)
clf.fit(X_tr, y_tr, sample_weight=weigths_train, eval_set=d_eval_set, verbose=False)
if print_report:
print(f'Data {dataNumber} Classification Report:\n')
print('Training Data:\n', classification_report(y_tr, clf.predict(X_tr)))
print('Testing Data:\n', classification_report(y_te, clf.predict(X_te)))
if save_model:
clf.save_model(f'saved_model_history/xgb_data{dataNumber}_{model_name}.json')
return clf
def plot_ROC(dataNumber, X_tr, y_tr, X_te, y_te, model, model_name, save=0):
"""
This is a function to draw an ROC curve overlaying training and testing results for selected parameters.
dataNumber: # for Data file in use (1, 2, 3, 4, 5)
X_tr: training data
y_tr: training labels
X_te: testing data
y_te: testinglabels
model: Classifier model, previously fit on training data
model_name: Title of model
save: Bool parameter to control saving the plot
"""
fig, ax = plt.subplots(figsize=(10, 8))
#model = XGBClassifier(**xgbParams)
#model.fit(X_tr, y_tr)
y_train_pred = model.predict(X_tr)
y_train_prob = model.predict_proba(X_tr) #Probability estimates for each class
fpr_train, tpr_train, thresholds_train = roc_curve(y_tr, y_train_prob[:,1])
auc_train = round(auc(fpr_train, tpr_train),3)
f1_train = round(f1_score(y_tr, y_train_pred),3)
recall_train = round(recall_score(y_tr, y_train_pred),3)
precision_train = round(precision_score(y_tr, y_train_pred),3)
accuracy_train = round(accuracy_score(y_tr, y_train_pred),3)
y_test_pred = model.predict(X_te)
y_test_prob = model.predict_proba(X_te) #Probability estimates for each class
fpr_test, tpr_test, thresholds_test = roc_curve(y_te, y_test_prob[:,1])
auc_test = round(auc(fpr_test, tpr_test),3)
f1_test = round(f1_score(y_te, y_test_pred),3)
recall_test = round(recall_score(y_te, y_test_pred),3)
precision_test = round(precision_score(y_te, y_test_pred),3)
accuracy_test = round(accuracy_score(y_te, y_test_pred),3)
label_train = f"Train: prec={precision_train}, rec={recall_train}, f1={f1_train}, acc={accuracy_train}, AUC={auc_train}"
label_test = f"Test: prec={precision_test}, rec={recall_test}, f1={f1_test}, acc={accuracy_test}, AUC={auc_test}"
ax.plot(fpr_train, tpr_train, lw=2, linestyle='dashed', label=label_train)
ax.plot(fpr_test, tpr_test, lw=2, label=label_test)
ax.plot([0, 1], [0, 1], color='0.7', lw=2, linestyle='-.')
ax.set_xlim([0.0, 1.0])
ax.set_ylim([0.0, 1.05])
#ax.set_yticks([i/20.0 for i in range(21)])
#ax.set_xticks([i/20.0 for i in range(21)])
ax.set_xlabel('False Positive Rate (FPR)', fontsize=14)
ax.set_ylabel('True Positive Rate (TPR)', fontsize=14)
ax.set_title(f'ROC Curve for Data {dataNumber}, {model_name}', fontsize=14)
ax.legend(loc='auto', fontsize=14)
if save:
plt.savefig(f'figures/ROC_data{dataNumber}_{model_name}.png')
def plot_logloss(dataNumber, model, model_name, save=0):
"""
This is a function to plot the Log Loss.
Returns to None.
dataNumber: # for Data file in use (1, 2, 3, 4, 5)
model: Trained model
model_name: Title of the model
save: Bool parameter to control saving the plot
"""
logloss_results = model.evals_result()
epochs = len(logloss_results['validation_0']['logloss'])
x_axis = range(0, epochs)
fig, ax = plt.subplots(figsize=(8,6))
ax.plot(x_axis, logloss_results['validation_0']['logloss'], label='Train')
ax.plot(x_axis, logloss_results['validation_1']['logloss'], label='Test')
ax.set_xlabel('Epochs', fontsize=14)
ax.set_ylabel('Log Loss',fontsize=14)
ax.set_title(f'Data {dataNumber}, Log Loss for {model_name}', fontsize=14)
ax.legend(loc='auto', fontsize=14)
if save:
plt.savefig(f'figures/LogLoss_data{dataNumber}_{model_name}.png')
def scan_xgb_ROC_metrics(dataNumber, X_tr, y_tr, X_te, y_te, xgbParams, scanParam, scanList, weights=0, plot=1, save=0):
"""
This is a function to scan over XGBClassifier parameters.
It creates two figures:
1) ROC curve overlaying training and testing results for all scanned values
2) Evaluation metrics overlaying training and testing results for all scanned values.
Returns to a dataframe of listing evaluation metrics.
dataNumber: # for Data file in use (1, 2, 3, 4, 5)
X_tr: training data
y_tr: training labels
X_te: testing data
y_te: testinglabels
xgbParams: XGBClassifier parameters used to create the model
scanParam: the XGBClassifier parameter, which will be scanned
scanList: the list of the values to be scanned; any size is OK.
weights: Bool parameter to use sample_weights or not.
plot: Bool parameter to control creating the plot
save: Bool parameter to control saving the plot
"""
if plot:
fig, (ax, ax2) = plt.subplots(1, 2, figsize=(20, 8))
cp = sns.color_palette()
#cp = ['red', 'orange', 'yellow', 'green', 'blue', 'purple']
cp = sns.color_palette()
model_scores_list = []
weigths_train = None
if weights:
print('Sample weights are used!')
weigths_train = class_weight.compute_sample_weight(class_weight='balanced', y=y_tr)
for i,s in enumerate(scanList):
xgbParams[scanParam]=s
clf = XGBClassifier(**xgbParams)
clf.fit(X_tr, y_tr, sample_weight=weigths_train)
y_train_pred = clf.predict(X_tr)
y_train_prob = clf.predict_proba(X_tr) #Probability estimates for each class
fpr_train, tpr_train, thresholds_train = roc_curve(y_tr, y_train_prob[:,1])
auc_train = round(auc(fpr_train, tpr_train),3)
f1_train = round(f1_score(y_tr, y_train_pred),3)
recall_train = round(recall_score(y_tr, y_train_pred),3)
precision_train = round(precision_score(y_tr, y_train_pred),3)
accuracy_train = round(accuracy_score(y_tr, y_train_pred),3)
y_test_pred = clf.predict(X_te)
y_test_prob = clf.predict_proba(X_te) #Probability estimates for each class
fpr_test, tpr_test, thresholds_test = roc_curve(y_te, y_test_prob[:,1])
auc_test = round(auc(fpr_test, tpr_test),3)
f1_test = round(f1_score(y_te, y_test_pred),3)
recall_test = round(recall_score(y_te, y_test_pred),3)
precision_test = round(precision_score(y_te, y_test_pred),3)
accuracy_test = round(accuracy_score(y_te, y_test_pred),3)
prec_diff_sc = (precision_train - precision_test)/precision_test
rec_diff_sc = (recall_train - recall_test)/recall_test
f1_diff_sc = (f1_train - f1_test)/f1_test
#overfit_measure = np.sqrt((1/3)*(prec_diff_sc**2 + rec_diff_sc**2 + f1_diff_sc**2))
overfit_measure = np.mean([prec_diff_sc, rec_diff_sc, f1_diff_sc])
fit_scores_train = {'Params': f'{scanParam}={s} Train ',
'precision': precision_train,
'recall': recall_train,
'f1': f1_train,
'accuracy': accuracy_train,
'auc': auc_train,
'prec_diff_sc': prec_diff_sc,
'rec_diff_sc': rec_diff_sc,
'f1_diff_sc': f1_diff_sc,
'overfit_measure': overfit_measure,
}
fit_scores_test = {'Params': f'Test',
'precision': precision_test,
'recall': recall_test,
'f1': f1_test,
'accuracy': accuracy_test,
'auc': auc_test,
}
model_scores_list.append(fit_scores_train)
model_scores_list.append(fit_scores_test)
if plot:
ax.plot(fpr_train, tpr_train, lw=2, color=cp[i], linestyle='dashed', label=f"Train, {scanParam}={s}, AUC={auc_train}")
ax.plot(fpr_test, tpr_test, lw=2, color=cp[i], label=f"Test, {scanParam}={s}, AUC={auc_test}")
model_scores_df = pd.DataFrame(model_scores_list)
model_scores_df = model_scores_df.set_index('Params')
#print(model_scores_df)
if plot:
#ax.plot([0, 1], [0, 1], color='0.7', lw=2, linestyle='-.')
ax.set_xlabel('False Positive Rate', fontsize=14)
ax.set_ylabel('True Positive Rate', fontsize=14)
ax.set_title(f"ROC Curve for Data {dataNumber}, Scan '{scanParam}' ", fontsize=14)
ax.legend(loc='auto', fontsize=13)
ax.set_xlim([0.0, 1.0])
ax.set_ylim([0.5, 1.05])
if plot:
select_row_train = [i*2 for i in range(0, len(scanList))]
select_row_test = [i*2+1 for i in range(0, len(scanList))]
rec_train_list = model_scores_df['recall'].iloc[select_row_train]
rec_test_list = model_scores_df['recall'].iloc[select_row_test]
f1_train_list = model_scores_df['f1'].iloc[select_row_train]
f1_test_list = model_scores_df['f1'].iloc[select_row_test]
prec_train_list = model_scores_df['precision'].iloc[select_row_train]
prec_test_list = model_scores_df['precision'].iloc[select_row_test]
acc_train_list = model_scores_df['accuracy'].iloc[select_row_train]
acc_test_list = model_scores_df['accuracy'].iloc[select_row_test]
ax2.plot(scanList, rec_train_list, 'ro--', label="Recall Train")
ax2.plot(scanList, rec_test_list, 'ro-', label="Recall Test")
ax2.plot(scanList, f1_train_list, 'go--', label="F1-score Train")
ax2.plot(scanList, f1_test_list, 'go-', label="F1-score Test")
ax2.plot(scanList, prec_train_list, 'yo--', label="Precision Train")
ax2.plot(scanList, prec_test_list, 'yo-', label="Precision Test")
ax2.plot(scanList, acc_train_list, 'bo--', label="Accuracy Train")
ax2.plot(scanList, acc_test_list, 'bo-', label="Accuracy Test")
ax2.set_xlabel(scanParam, fontsize=14)
ax2.set_ylabel('Metric Value', fontsize=14)
ax2.set_title(f"Evaluation Metrics for Data {dataNumber}, Scan '{scanParam}' ", fontsize=14)
ax2.legend(loc='auto', fontsize=13)
ax2.set_ylim([0, 1.05])
if save and plot:
plt.savefig(f'figures/ROC_Metrics_d{dataNumber}_{scanParam}.png')
return model_scores_df
def scan_xgb_logloss_metrics(dataNumber, X_tr, y_tr, X_te, y_te, xgbParams, scanParam, scanList, weights=0, plot=1, save=0):
"""
This is a function to scan over XGBClassifier parameters.
It creates two figures:
1) Log Loss overlaying training and testing results for all scanned values
2) Evaluation metrics overlaying training and testing results for all scanned values.
Returns to a table of listing evaluation metrics.
dataNumber: # for Data file in use (1, 2, 3, 4, 5)
X_tr: training data
y_tr: training labels
X_te: testing data
y_te: testinglabels
xgbParams: XGBClassifier parameters used to create the model
scanParam: the XGBClassifier parameter, which will be scanned
scanList: the list of the values to be scanned; any size is OK.
weights: Bool parameter to use sample_weights or not.
plot: Bool parameter to control creating the plot
save: Bool parameter to control saving the plot
"""
if plot:
fig, (ax, ax2) = plt.subplots(1, 2, figsize=(20, 8))
cp = sns.color_palette()
model_scores_list = []
weigths_train = None
if weights:
print('Sample weights are used!')
weigths_train = class_weight.compute_sample_weight(class_weight='balanced', y=y_tr)
d_eval_set = [(X_tr, y_tr), (X_te, y_te)]
for i,s in enumerate(scanList):
xgbParams[scanParam]=s
clf = XGBClassifier(**xgbParams)
clf.fit(X_tr, y_tr, sample_weight=weigths_train, eval_set=d_eval_set, verbose=False)
y_train_pred = clf.predict(X_tr)
f1_train = round(f1_score(y_tr, y_train_pred),3)
recall_train = round(recall_score(y_tr, y_train_pred),3)
precision_train = round(precision_score(y_tr, y_train_pred),3)
accuracy_train = round(accuracy_score(y_tr, y_train_pred),3)
y_test_pred = clf.predict(X_te)
f1_test = round(f1_score(y_te, y_test_pred),3)
recall_test = round(recall_score(y_te, y_test_pred),3)
precision_test = round(precision_score(y_te, y_test_pred),3)
accuracy_test = round(accuracy_score(y_te, y_test_pred),3)
prec_diff_sc = (precision_train - precision_test)/precision_test
rec_diff_sc = (recall_train - recall_test)/recall_test
f1_diff_sc = (f1_train - f1_test)/f1_test
overfit_measure = np.mean([prec_diff_sc, rec_diff_sc, f1_diff_sc])
logloss_results = clf.evals_result()
logloss_results_train = logloss_results['validation_0']['logloss']
logloss_results_test = logloss_results['validation_1']['logloss']
logloss_train = round(logloss_results_train[-1], 3)
logloss_test = round(logloss_results_test[-1], 3)
fit_scores_train = {'Params': f'{scanParam}={s} Train ',
'precision': precision_train,
'recall': recall_train,
'f1': f1_train,
'accuracy': accuracy_train,
'logloss': logloss_train
#'prec_diff_sc': prec_diff_sc,
#'rec_diff_sc': rec_diff_sc,
#'f1_diff_sc': f1_diff_sc,
#'overfit_measure': overfit_measure,
}
fit_scores_test = {'Params': f'Test',
'precision': precision_test,
'recall': recall_test,
'f1': f1_test,
'accuracy': accuracy_test,
'logloss': logloss_test,
}
model_scores_list.append(fit_scores_train)
model_scores_list.append(fit_scores_test)
if plot:
# Plot logloss
if i==0:
epochs = len(logloss_results_train)
x_axis = range(0, epochs)
ax.plot(x_axis, logloss_results_train, lw=2, color=cp[i], linestyle='dashed', label=f"Train, {scanParam}={s}")
ax.plot(x_axis, logloss_results_test, lw=2, color=cp[i], label=f"Test, {scanParam}={s}")
model_scores_df = pd.DataFrame(model_scores_list)
model_scores_df = model_scores_df.set_index('Params')
if plot:
# logloss plot settings
ax.set_xlabel('Epochs', fontsize=14)
ax.set_ylabel('Log Loss', fontsize=14)
ax.set_title(f'Data {dataNumber}, Log Loss', fontsize=14)
ax.legend(loc='auto', fontsize=13)
#ax.set_xlim([0.0, 1.0])
ax.set_ylim([0.0, 1.0])
#Plot metrics
select_row_train = [i*2 for i in range(0, len(scanList))]
select_row_test = [i*2+1 for i in range(0, len(scanList))]
rec_train_list = model_scores_df['recall'].iloc[select_row_train]
rec_test_list = model_scores_df['recall'].iloc[select_row_test]
f1_train_list = model_scores_df['f1'].iloc[select_row_train]
f1_test_list = model_scores_df['f1'].iloc[select_row_test]
prec_train_list = model_scores_df['precision'].iloc[select_row_train]
prec_test_list = model_scores_df['precision'].iloc[select_row_test]
acc_train_list = model_scores_df['accuracy'].iloc[select_row_train]
acc_test_list = model_scores_df['accuracy'].iloc[select_row_test]
ax2.plot(scanList, rec_train_list, 'ro--', label="Recall Train")
ax2.plot(scanList, rec_test_list, 'ro-', label="Recall Test")
ax2.plot(scanList, f1_train_list, 'go--', label="F1-score Train")
ax2.plot(scanList, f1_test_list, 'go-', label="F1-score Test")
ax2.plot(scanList, prec_train_list, 'yo--', label="Precision Train")
ax2.plot(scanList, prec_test_list, 'yo-', label="Precision Test")
ax2.plot(scanList, acc_train_list, 'bo--', label="Accuracy Train")
ax2.plot(scanList, acc_test_list, 'bo-', label="Accuracy Test")
ax2.set_xlabel(scanParam, fontsize=14)
ax2.set_ylabel('Metric Value', fontsize=14)
ax2.set_title(f"Evaluation Metrics for Data {dataNumber}, Scan '{scanParam}' ", fontsize=14)
ax2.legend(loc='auto', fontsize=14)
ax2.set_ylim([0, 1.05])
if save and plot:
plt.savefig(f'figures/LogLoss_Metrics_d{dataNumber}_{scanParam}.png')
return model_scores_df
def compare_models(dataNumber, X_tr, y_tr, X_te, y_te, model_list, model_names_list, title, plot=1, save=0):
"""
This is a function to compare models.
It creates ROC curve and calculate metrics:
Returns to dataframe of listing evaluation metrics.
dataNumber: # for Data file in use (1, 2, 3, 4, 5)
X_tr: training data
y_tr: training labels
X_te: testing data
y_te: testinglabels
model_list: list of the models
model_names_list: list of the title of the models
title: title of comparison plot
plot: Bool parameter to control creating the plot
save: Bool parameter to control saving the plot
"""
if plot:
fig, ax = plt.subplots(figsize=(10, 8))
cp = sns.color_palette()
model_scores_list = []
for i,clf in enumerate(model_list):
y_train_pred = clf.predict(X_tr)
y_train_prob = clf.predict_proba(X_tr) #Probability estimates for each class
fpr_train, tpr_train, thresholds_train = roc_curve(y_tr, y_train_prob[:,1])
auc_train = round(auc(fpr_train, tpr_train),3)
f1_train = round(f1_score(y_tr, y_train_pred),3)
recall_train = round(recall_score(y_tr, y_train_pred),3)
precision_train = round(precision_score(y_tr, y_train_pred),3)
accuracy_train = round(accuracy_score(y_tr, y_train_pred),3)
y_test_pred = clf.predict(X_te)
y_test_prob = clf.predict_proba(X_te) #Probability estimates for each class
fpr_test, tpr_test, thresholds_test = roc_curve(y_te, y_test_prob[:,1])
auc_test = round(auc(fpr_test, tpr_test),3)
f1_test = round(f1_score(y_te, y_test_pred),3)
recall_test = round(recall_score(y_te, y_test_pred),3)
precision_test = round(precision_score(y_te, y_test_pred),3)
accuracy_test = round(accuracy_score(y_te, y_test_pred),3)
fit_scores_train = {'Params': f'{model_names_list[i]} Train ',
'precision': precision_train,
'recall': recall_train,
'f1': f1_train,
'accuracy': accuracy_train,
'auc': auc_train,
}
fit_scores_test = {'Params': f'Test',
'precision': precision_test,
'recall': recall_test,
'f1': f1_test,
'accuracy': accuracy_test,
'auc': auc_test,
}
model_scores_list.append(fit_scores_train)
model_scores_list.append(fit_scores_test)
if plot:
#label_train = f"{model_names_list[i]} Train: prec={precision_train}, rec={recall_train}, f1={f1_train}, acc={accuracy_train}, AUC={auc_train}"
#label_test = f"{model_names_list[i]} Test: prec={precision_test}, rec={recall_test}, f1={f1_test}, acc={accuracy_test}, AUC={auc_test}"
label_train = f"{model_names_list[i]} Train"
label_test = f"{model_names_list[i]} Test"
ax.plot(fpr_train, tpr_train, lw=2, color=cp[i], linestyle='dashed', label=label_train)
ax.plot(fpr_test, tpr_test, lw=2, color=cp[i], label=label_test)
model_scores_df = pd.DataFrame(model_scores_list)
model_scores_df = model_scores_df.set_index('Params')
#print(model_scores_df)
if plot:
#ax.plot([0, 1], [0, 1], color='0.7', lw=2, linestyle='-.')
ax.set_xlabel('False Positive Rate', fontsize=14)
ax.set_ylabel('True Positive Rate', fontsize=14)
ax.set_title(f"ROC Curve for Data {dataNumber}, Model Comparison", fontsize=14)
ax.legend(loc='auto', fontsize=13)
ax.set_xlim([0.0, 1.0])
ax.set_ylim([0.5, 1.05])
if save:
plt.savefig(f'figures/ROC_modelCompare_{title}_d{dataNumber}.png')
return model_scores_df
def compare_datafiles_perf(df_list, xgbParams, title, weights, plot=1, save=0):
"""
This is a function to compare datsets performance.
It creates ROC curve and calculate metrics:
Returns to dataframe of listing evaluation metrics.
df_list: # DataFrame list to be used for comparison
xgbParams: XGBClassifier parameters used to create the model
weights: Bool parameter to use sample_weights or not.
title: title of comparison plot
plot: Bool parameter to control creating the plot
save: Bool parameter to control saving the plot
"""
print(f'------{title}------')
if plot:
fig, ax = plt.subplots(figsize=(10, 8))
cp = sns.color_palette()
model_scores_list = []
for i,df in enumerate(df_list):
#Pre-process data
y = df['class']
X = df.drop('class', axis=1)
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.20, random_state=42)
scaler = StandardScaler()
X_tr = scaler.fit_transform(X_train)
X_te = scaler.transform(X_test)
y_tr = y_train.to_numpy()
y_te = y_test.to_numpy()
#Train model
weigths_train = None
if weights:
print('Sample weights are used!')
weigths_train = class_weight.compute_sample_weight(class_weight='balanced', y=y_tr)
d_eval_set = [(X_tr, y_tr), (X_te, y_te)]
clf = XGBClassifier(**xgbParams)
clf.fit(X_tr, y_tr, sample_weight=weigths_train, eval_set=d_eval_set, verbose=False)
#Calculate evaluation metrics
y_train_pred = clf.predict(X_tr)
y_train_prob = clf.predict_proba(X_tr) #Probability estimates for each class
fpr_train, tpr_train, thresholds_train = roc_curve(y_tr, y_train_prob[:,1])
auc_train = round(auc(fpr_train, tpr_train),3)
f1_train = round(f1_score(y_tr, y_train_pred),3)
recall_train = round(recall_score(y_tr, y_train_pred),3)
precision_train = round(precision_score(y_tr, y_train_pred),3)
accuracy_train = round(accuracy_score(y_tr, y_train_pred),3)
y_test_pred = clf.predict(X_te)
y_test_prob = clf.predict_proba(X_te) #Probability estimates for each class
fpr_test, tpr_test, thresholds_test = roc_curve(y_te, y_test_prob[:,1])
auc_test = round(auc(fpr_test, tpr_test),3)
f1_test = round(f1_score(y_te, y_test_pred),3)
recall_test = round(recall_score(y_te, y_test_pred),3)
precision_test = round(precision_score(y_te, y_test_pred),3)
accuracy_test = round(accuracy_score(y_te, y_test_pred),3)
fit_scores_train = {'Data': f'Data {i+1}',
'Sample': 'Train',
'precision': precision_train,
'recall': recall_train,
'f1': f1_train,
'accuracy': accuracy_train,
'auc': auc_train,
}
fit_scores_test = {'Data': f'Data {i+1}',
'Sample': 'Test',
'precision': precision_test,
'recall': recall_test,
'f1': f1_test,
'accuracy': accuracy_test,
'auc': auc_test,
}
model_scores_list.append(fit_scores_train)
model_scores_list.append(fit_scores_test)
if plot:
#label_train = f"{model_names_list[i]} Train: prec={precision_train}, rec={recall_train}, f1={f1_train}, acc={accuracy_train}, AUC={auc_train}"
#label_test = f"{model_names_list[i]} Test: prec={precision_test}, rec={recall_test}, f1={f1_test}, acc={accuracy_test}, AUC={auc_test}"
label_train = f"Data {i+1} Train"
label_test = f"Data {i+1} Test"
ax.plot(fpr_train, tpr_train, lw=2, color=cp[i], linestyle='dashed', label=label_train)
ax.plot(fpr_test, tpr_test, lw=2, color=cp[i], label=label_test)
#Create DataFrame
model_scores_df = pd.DataFrame(model_scores_list)
#Calculate averages
select_row_train = [i*2 for i in range(0, len(df_list))]
select_row_test = [i*2+1 for i in range(0, len(df_list))]
rec_train_list = model_scores_df['recall'].iloc[select_row_train]
rec_test_list = model_scores_df['recall'].iloc[select_row_test]
f1_train_list = model_scores_df['f1'].iloc[select_row_train]
f1_test_list = model_scores_df['f1'].iloc[select_row_test]
prec_train_list = model_scores_df['precision'].iloc[select_row_train]
prec_test_list = model_scores_df['precision'].iloc[select_row_test]
acc_train_list = model_scores_df['accuracy'].iloc[select_row_train]
acc_test_list = model_scores_df['accuracy'].iloc[select_row_test]
auc_train_list = model_scores_df['auc'].iloc[select_row_train]
auc_test_list = model_scores_df['auc'].iloc[select_row_test]
train_avg_dic = {'Data': 'Average',
'Sample':'Train',
'precision': np.mean(prec_train_list),
'recall': np.mean(rec_train_list),
'f1': np.mean(f1_train_list),
'accuracy': np.mean(acc_train_list),
'auc': np.mean(auc_train_list),
}
test_avg_dic = {'Data': 'Average',
'Sample': 'Test',
'precision': np.mean(prec_test_list),
'recall': np.mean(rec_test_list),
'f1': np.mean(f1_test_list),
'accuracy': np.mean(acc_test_list),
'auc': np.mean(auc_test_list)
}
#Add average rows to DataFrame
model_scores_df = model_scores_df.append([train_avg_dic, test_avg_dic], ignore_index=True)
#Set Dataframe index
model_scores_df = model_scores_df.set_index('Data')
if plot:
#ax.plot([0, 1], [0, 1], color='0.7', lw=2, linestyle='-.')
ax.set_xlabel('False Positive Rate', fontsize=14)
ax.set_ylabel('True Positive Rate', fontsize=14)
ax.set_title(f"ROC Curve, Dataset Comparison for {title}", fontsize=14)
ax.legend(loc='auto', fontsize=13)
ax.set_xlim([0.0, 1.0])
ax.set_ylim([0.5, 1.05])
if save:
plt.savefig(f'figures/ROC_dataCompare_{title}.png')
return model_scores_df
def plot_compare_model_metricsAvg(metrics_df_list, model_names_list, save=0):
fig, axes = plt.subplots(2, 2, figsize=(20, 16))
cp = sns.color_palette()
metrics=['precision', 'recall', 'f1', 'auc']
data_list=['Data 1', 'Data 2', 'Data 3', 'Data 4', 'Data 5']
for i,df in enumerate(metrics_df_list):
for m,ax in enumerate(axes.flat):
ax.plot(df[df['Sample']=='Train'].iloc[:-1][metrics[m]],
color=cp[i], marker='o', linestyle='dashed', label=f'{model_names_list[i]} Train')
ax.plot(df[df['Sample']=='Test'].iloc[:-1][metrics[m]],
color=cp[i],marker='o', label=f'{model_names_list[i]} Test')
ax.set_xticklabels(data_list, fontsize=14)
ax.set_ylabel(f'{metrics[m]} Average', fontsize=16)
ax.legend(loc='auto', fontsize=13)
ax.set_ylim([0, 1.05])
if save:
plt.savefig(f'figures/Metrics_CompareModels_AllData.png')
def plot_scan6_xgb(dataNumber, X_tr, y_tr, X_te, y_te, xgbParams, scanParam, scanList, weights, save=0):
"""
This is a function to scan over XGBClassifier parameters.
It creates 6 figures /ROC curve overlaying training and testing results. One figure for each scan value.
Returns to a table of listing evaluation metrics.
dataNumber: Data file in use
X_tr: training data
y_tr: training labels
X_te: testing data
y_te: testinglabels
xgbParams: XGBClassifier parameters used to create the model
scanParam: the XGBClassifier parameter, which will be scannes
scanList: the list of the values to be scanned; The required list size is 6.
weights: Bool parameter to use sample_weights or not.
save: Bool parameter to control saving the plot
"""
fig, axes = plt.subplots(3, 2, figsize=(20, 20))
#plt.tight_layout(pad=5)
model_scores_list = []
weigths_train = None
if weights:
print('Sample weights are used!')
weigths_train = class_weight.compute_sample_weight(class_weight='balanced', y=y_tr)
for ax, s in zip(axes.flat, scanList):
xgbParams[scanParam]=s
clf = XGBClassifier(**xgbParams)
clf.fit(X_tr, y_tr, sample_weight=weigths_train)
y_train_pred = clf.predict(X_tr)
y_train_prob = clf.predict_proba(X_tr) #Probability estimates for each class
fpr_train, tpr_train, thresholds_train = roc_curve(y_tr, y_train_prob[:,1])
auc_train = round(auc(fpr_train, tpr_train),3)
f1_train = round(f1_score(y_tr, y_train_pred),3)
recall_train = round(recall_score(y_tr, y_train_pred),3)
precision_train = round(precision_score(y_tr, y_train_pred),3)
accuracy_train = round(accuracy_score(y_tr, y_train_pred),3)
y_test_pred = clf.predict(X_te)
y_test_prob = clf.predict_proba(X_te) #Probability estimates for each class
fpr_test, tpr_test, thresholds_test = roc_curve(y_te, y_test_prob[:,1])
auc_test = round(auc(fpr_test, tpr_test),3)
f1_test = round(f1_score(y_te, y_test_pred),3)
recall_test = round(recall_score(y_te, y_test_pred),3)
precision_test = round(precision_score(y_te, y_test_pred),3)
accuracy_test = round(accuracy_score(y_te, y_test_pred),3)
fit_scores_train = {'Params': f'{scanParam}={s} Train ',
'accuracy': accuracy_train,
'precision': precision_train,
'recall': recall_train,
'f1': f1_train,
'auc': auc_train
}
fit_scores_test = {'Params': f'Test',
'accuracy': accuracy_test,
'precision': precision_test,
'recall': recall_test,
'f1': f1_test,
'auc': auc_test
}
model_scores_list.append(fit_scores_train)
model_scores_list.append(fit_scores_test)
ax.plot(fpr_train, tpr_train, lw=2, label=f'Train: acc={accuracy_train}, prec={precision_train}, rec={recall_train}, f1={f1_train}, AUC={auc_train}')
ax.plot(fpr_test, tpr_test, lw=2, label=f'Test: acc={accuracy_test}, prec={precision_test}, rec={recall_test}, f1={f1_test}, AUC={auc_test}')
ax.plot([0, 1], [0, 1], color='0.7', lw=2, linestyle='-.')
ax.set_xlim([0.0, 1.0])
ax.set_ylim([0.0, 1.05])
ax.set_yticks([i/20.0 for i in range(21)])
ax.set_xticks([i/20.0 for i in range(21)])
ax.set_xlabel('False Positive Rate', fontsize=14)
ax.set_ylabel('True Positive Rate', fontsize=14)
ax.set_title(f'ROC Curve for Data {dataNumber}, {scanParam}={s}', fontsize=14)
ax.legend(loc='auto', fontsize=13)
model_scores_df = pd.DataFrame(model_scores_list)
model_scores_df = model_scores_df.set_index('Params')
#print(model_scores_df)
if save:
plt.savefig(f'figures/ROC_Curve_d{dataNumber}_{scanParam}.png')
return model_scores_df