-
Notifications
You must be signed in to change notification settings - Fork 0
/
Code_Dec_17_2022.py
624 lines (592 loc) · 31.1 KB
/
Code_Dec_17_2022.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
# -*- coding: utf-8 -*-
"""
@author: Toktam Dehghani
Email:dehghani.toktam@mail.um.ac.ir
Website: www.SmartDxCloud.ir
"""
import warnings
warnings.filterwarnings("ignore")
import math
import scipy
from scipy.stats import norm
import matplotlib.pyplot as pyplot
from sklearn import model_selection
import pandas as pd
import pingouin as pg
import seaborn as sns
import matplotlib.pyplot as plt
from pandas import plotting
from sklearn import svm
import numpy as np
from sklearn.inspection import permutation_importance
from sklearn.ensemble import RandomForestClassifier
from sklearn.model_selection import train_test_split
from collections import Counter
from sklearn import svm
import time
from xgboost import XGBClassifier
from sklearn.ensemble import GradientBoostingClassifier
from sklearn.linear_model import LogisticRegression
from sklearn.svm import LinearSVC
from sklearn.model_selection import GridSearchCV
from sklearn.naive_bayes import GaussianNB
from sklearn.neighbors import KNeighborsClassifier
from sklearn.tree import DecisionTreeClassifier
from sklearn.ensemble import BaggingClassifier
from sklearn.ensemble import AdaBoostClassifier
from sklearn.ensemble import VotingClassifier
from sklearn.ensemble import RandomForestRegressor
from sklearn.metrics import roc_auc_score
from sklearn.metrics import roc_curve
from sklearn.calibration import calibration_curve
import matplotlib.lines as mlines
from sklearn.metrics import (brier_score_loss, precision_score, recall_score,f1_score,auc)
from sklearn.metrics import matthews_corrcoef
from sklearn.metrics import precision_recall_curve
from sklearn.metrics import average_precision_score
from sklearn.metrics import accuracy_score
from sklearn.metrics import classification_report
from sklearn.model_selection import cross_val_score
from sklearn.neural_network import MLPClassifier
from sklearn import tree
from sklearn.calibration import CalibratedClassifierCV
from sklearn.metrics import confusion_matrix
from sklearn.model_selection import StratifiedKFold
from sklearn.metrics import mean_squared_error
from sklearn.metrics import r2_score
from sklearn.metrics import ConfusionMatrixDisplay
from collections import Counter
import researchpy as rp
import scipy.stats as stats
import shap
#----------------------------------------------------------------- Evaluation
def plot_Calibration():
print ('Calibration plots')
print ('Calibration plots (reliability curve 2)', file=sourceFile)
fig=plt.figure(figsize=(15, 15))
ax1 = plt.subplot2grid((3, 1), (0, 0), rowspan=2)
ax2 = plt.subplot2grid((3, 1), (2, 0))
ax1.plot([0, 1], [0, 1], "k:", label="Perfectly calibrated")
for name ,clf in models:
#clf=clf.fit(X_train, Y_train)
if hasattr(clf, "predict_proba"):
prob_pos = clf.predict_proba(X_test)[:, 1]
else: # use decision function
prob_pos = clf.decision_function(X_test)
prob_pos = \
(prob_pos - prob_pos.min()) / (prob_pos.max() - prob_pos.min())
fraction_of_positives, mean_predicted_value = \
calibration_curve(Y_test, prob_pos, n_bins=5)
clf_score = brier_score_loss(Y_test, prob_pos, pos_label=1)
ax1.plot(mean_predicted_value, fraction_of_positives, "s-", lw=3, label="%s (BS:=%1.3f)" % (name, clf_score))
ax2.hist(prob_pos, range=(0, 1), bins=5, label=name, histtype="step", lw=3)
ax1.set_ylabel("Predicted Probability",fontsize= 28)
ax1.set_ylim([-0.05, 1.05])
ax1.legend(loc="center left", ncol=1,fontsize= 24, bbox_to_anchor=(1.05, 0.5))
#ax1.set_title('Calibration plots (reliability curve)',fontsize= 30)
ax2.set_xlabel("True Probability of Passing Exam",fontsize= 28)
ax2.set_ylabel("Count",fontsize= 28)
ax2.legend(loc="center left", ncol=1,fontsize= 24, bbox_to_anchor=(1.05, 0.5))
plt.tight_layout()
plt.show()
fig.savefig('Calibration plots (reliability curve)2'+'.svg', format='svg', dpi=1200)
def plot_roc():
print ('Receiver operating characteristic (ROC)')
print ('Receiver operating characteristic (ROC)', file=sourceFile)
#sns.set(style="white",font='sans-serif',font_scale=1.5)
fig, axs = plt.subplots(figsize=(10,10))
for name, model in models:
#model=model.fit(X_train,Y_train)
y_predict=model.predict(X_test)
y_predict=y_predict.ravel()
LR_roc_auc=roc_auc_score(Y_test,y_predict.round(),average='macro')
Y_test_predict=model.predict_proba(X_test)
fpr,tpr,thresholds=roc_curve(Y_test,Y_test_predict[:,1])
axs.plot(fpr,tpr,label=name+' (AUC ='+str(round(LR_roc_auc,2))+")")
axs.plot([0, 1], [0, 1], linestyle='--', lw=2, color='r', label='Chance', alpha=.8)
axs.set_xlabel("1 - specificity",fontsize= 30)
axs.set_ylabel("Sensitivity",fontsize= 30)
axs.set_ylim([0.0,1.05])
axs.set_xlim([0.0,1.05])
axs.legend(loc="center left", ncol=1,fontsize= 24, bbox_to_anchor=(1.05, 0.5))
fig.savefig('prediction_ROC_AUC'+'.svg', format='svg', dpi=1200)
def plot_Precision_Recall():
print ('Precision-Recall curve')
print ('Precision-Recall curve', file=sourceFile)
fig1, axs1 = plt.subplots(figsize=(10,10))
for name, model in models:
#model.fit(X_train, Y_train)
y_score = model.predict(X_test)
# y_score=y_score.ravel()
Y_test_predict=model.predict_proba(X_test)
precision, recall, _ = precision_recall_curve(Y_test, Y_test_predict[:, 1].ravel())
lr_f1 =f1_score(Y_test, y_score.round(),average='weighted')
axs1.plot(recall, precision,label="%s (F1 = %.2f)" % (name, round(lr_f1,2) ))
axs1.plot([0, 1], [0, 1], linestyle='--', lw=2, color='r', label='Chance', alpha=.8)
axs1.set_ylim([0.0,1.05])
axs1.set_xlim([0.0,1.05])
axs1.set_ylabel('Precision',fontsize= 30)
axs1.set_xlabel('Recall',fontsize= 30)
axs1.legend(loc="center left", ncol=1,fontsize= 24, bbox_to_anchor=(1.05, 0.5))
fig1.savefig('Precision-Recall curve'+'.svg', format='svg', dpi=1200)
def save_output():
print ('save results of models')
print ('save results of models', file=sourceFile)
Y_test.to_csv(r'Y_test.csv')
for name, model in models:
Y_test_predict=np.round(abs(model.predict(X_test)))
Y_test_predict.tofile('Y-test_predict_'+name+'.csv',sep=',',format="%s")
def avaluate(model):
print("\n***********************\n Classification_report")
print("\n***********************\n Classification_report", file=sourceFile)
print("\nTraining", file=sourceFile)
y_score = model.predict(X_train).round()
y_score=y_score.ravel()
Y_train_predict=model.predict_proba(X_train)
print("roc_auc:: %1.3f\n" % roc_auc_score(Y_train, y_score,average='macro'), file=sourceFile)
print("Average Precision: %1.3f" % average_precision_score(Y_train, y_score,average='weighted'), file=sourceFile)
print("Precision: %1.3f" % precision_score(Y_train, y_score,average='weighted'), file=sourceFile)
print("Recall: %1.3f" % recall_score(Y_train, y_score,average='weighted'), file=sourceFile)
print("tn, fp, fn, tp \n" , file=sourceFile)
print(confusion_matrix (Y_train, y_score), file=sourceFile)
print("accuracy:: %1.3f" % accuracy_score(Y_train, y_score), file=sourceFile)
print("F1_weighted: %1.3f" % f1_score(Y_train, y_score,average='weighted'), file=sourceFile)
print("MCC:: %1.3f" % matthews_corrcoef(Y_train, y_score), file=sourceFile)
print("Brier scores: %1.3f (smaller is better)" % brier_score_loss(Y_train, Y_train_predict[:, 1],pos_label=1), file=sourceFile)
print(classification_report(Y_train, y_score), file=sourceFile)
print("\nTest", file=sourceFile)
y_score = model.predict(X_test).round()
y_score=y_score.ravel()
Y_test_predict=model.predict_proba(X_test)
print("roc_auc:: %1.3f" % roc_auc_score(Y_test, y_score,average='macro'), file=sourceFile)
print("Average Precision (area PR): %1.3f" % average_precision_score(Y_test, y_score,average='weighted'), file=sourceFile)
print("Precision: %1.3f" % precision_score(Y_test, y_score,average='weighted'), file=sourceFile)
print("Recall: %1.3f" % recall_score(Y_test, y_score,average='weighted'), file=sourceFile)
print("tn, fp, fn, tp" , file=sourceFile)
print(confusion_matrix (Y_test, y_score), file=sourceFile)
print("accuracy:: %1.3f" % accuracy_score(Y_test, y_score), file=sourceFile)
print("F1: %1.3f" % f1_score(Y_test, y_score ,average='weighted'), file=sourceFile)
print("MCC:: %1.3f" % matthews_corrcoef(Y_test, y_score), file=sourceFile)
print("Brier scores: %1.3f (smaller is better)" % brier_score_loss(Y_test, Y_test_predict[:, 1],pos_label=1), file=sourceFile)
print('Mean squared error: %.2f'% mean_squared_error(Y_test, y_score, squared=False), file=sourceFile)
print('Coefficient of determination: %.2f' % r2_score(Y_test, y_score), file=sourceFile)
print(classification_report(Y_test, y_score), file=sourceFile)
def plot_confusion_matrix():
print ('plot_confusion_matrix')
print ('plot_confusion_matrix', file=sourceFile)
for name, model in models:
y_score = model.predict(X_test)
y_score=y_score.ravel()
fig8 = plt.figure(figsize=(15,15))
cm=confusion_matrix(Y_test, y_score,normalize='all')
plt.plot=sns.heatmap(cm.round(2), annot=True, linewidths=.5, fmt='g',cmap="Spectral_r",cbar_kws={"orientation": "horizontal"} )
plt.suptitle('Confusion_matrix ('+ str(name) +')',fontsize= 30)
plt.xlabel('Predicted class',fontsize= 28)
plt.ylabel('Actual class',fontsize= 28)
fig8.savefig('confusion_matrix_'+str(name)+'.svg', format='svg', dpi=1200)
def plot_comparing_accurecy():
print ('Models comparision (accuracy)')
print ('Models comparision (accuracy)', file=sourceFile)
results=[]
names=[]
fig4 = plt.figure(figsize=(40,20))
for name, model in models:
cv = RepeatedStratifiedKFold(n_splits=10, n_repeats=10, random_state=12)
result=model_selection.cross_val_score(model,X_test, Y_test,cv=CV,scoring='accuracy',n_jobs=-1)
results.append(result)
names.append(name)
fig4=plt.figure()
fig4.suptitle('Models Accuracy Comparision',fontsize= 20)
ax=fig4.add_subplot(111)
plt.violinplot(results)
ax.set_xticklabels(names,fontsize= 12)
ax.set_yticklabels([],fontsize= 12)
plt.xlabel('Methods',fontsize= 14)
plt.ylabel('Accuracy',fontsize= 14)
fig4.savefig('Comparision_accurecy'+'.svg', format='svg', dpi=1200)
def plot_comparing_roc_auc():
print ('Models comparision (roc_auc)')
print ('Models comparision (roc_auc)', file=sourceFile)
results=[]
names=[]
fig44 = plt.figure(figsize=(20,20))
for name, model in models:
result=model_selection.cross_val_score(model,X_test, Y_test,cv=CV,scoring='roc_auc')
results.append(result)
names.append(name)
fig44=plt.figure()
fig44.suptitle('Models comparision (roc_auc)',fontsize= 28)
ax=fig44.add_subplot(111)
plt.boxplot(results,1, '')
ax.set_xticklabels(names,fontsize= 28)
ax.set_yticklabels(results,fontsize= 28)
plt.xticks(rotation=28)
fig44.savefig('Comparision_roc_auc'+'.svg', format='svg', dpi=1200)
#_________________________________________________________________________________________________________________________________
#_________________________________________________________________________________________________________________________________
#__________________________________________________ Load Data ________________________________________________________________________
#_________________________________________________________________________________________________________________________________
print("1.Load data")
report_file='log_file_2022.txt'
input_file="Sample.xlsx"
input_sheet='Sheet1'
sourceFile = open(report_file, 'w')
stu_info_payeh= pd.read_excel(input_file, sheet_name=input_sheet)
print(stu_info_payeh.info())
#_________________________________________________________________________________________________________________________________
#_________________________________________________________________________________________________________________________________
print("2. Dealing with missing values ")
dt=stu_info_payeh[['Anatomical Sciences ','Physiology','Biochemistry','Technical English Language','Bacteriology','Virology','Parasitology and Entomology','Mycology','Principles of Epidemiology','Public Health','General Knowledge','GPA','Age at Entrance ', 'Age at CMBSE','Gender', 'Residency Status', 'Entrance Semester', 'Type of Admission','Status in the CMBSE','Normalized CMBSE Score']]#,'No of Attemp']]
dt=dt.dropna(subset=['Status in the CMBSE'])
#---------------------------------------------------------- Normalization
print("3. Normalization ")
names=['Gender', 'Residency Status', 'Entrance Semester', 'Type of Admission']
for name in names:
df_one = pd.get_dummies(data=dt, columns=[str(name)])
df_two = pd.concat((df_one, dt[str(name)]), axis=1)
df_two = df_two.drop([str(name)], axis=1)
dt=df_two
dt.rename(columns = {'Residency Status_Local':'Residency Status'}, inplace = True)
dt.rename(columns = {'Entrance Semester_First':'Entrance Semester'}, inplace = True)
dt.rename(columns = {'Gender_Male':'Gender'}, inplace = True)
dt.rename(columns = {'Type of Admission_Daily Course':'Type of Admission'}, inplace = True)
dt[['Status in the CMBSE'] ]=dt[['Status in the CMBSE'] ].replace("Fail", 0)
dt[['Status in the CMBSE'] ]=dt[['Status in the CMBSE'] ].replace("Pass", 1)
#print(dt[['Status in the CMBSE']])
#----------------------------------------------------------- Apply Weights of courses
print("4. Apply Weights of courses ")
dt= dt[['Anatomical Sciences ','Physiology','Biochemistry','Technical English Language','Bacteriology','Virology','Parasitology and Entomology','Mycology','Principles of Epidemiology','Public Health','General Knowledge','GPA','Gender', 'Residency Status', 'Entrance Semester', 'Type of Admission','Status in the CMBSE']]
columns = ['Physiology', 'Biochemistry', 'Bacteriology',
'Parasitology and Entomology', 'Mycology', 'Virology',
'Anatomical Sciences ', 'Public Health',
'Principles of Epidemiology', 'Technical English Language',
'General Knowledge']
weights=[0.27,0.18,0.1,0.1,0.08,0.025,0.06,0.025,0.06,0.05,0.05]
for i in range (0, len(weights)):
dt[columns[i]]=dt[columns[i]]*weights[i]
#---------------------------------------------------------- Transformation
print("5. Transformation")
from sklearn.preprocessing import StandardScaler
from sklearn.preprocessing import MinMaxScaler
from sklearn.decomposition import PCA
ss_train = MinMaxScaler()#
dt[['Physiology', 'Biochemistry', 'Bacteriology',
'Parasitology and Entomology', 'Mycology', 'Virology',
'Anatomical Sciences ', 'Public Health',
'Principles of Epidemiology', 'Technical English Language',
'General Knowledge','GPA']]= ss_train.fit_transform(dt[['Physiology', 'Biochemistry', 'Bacteriology',
'Parasitology and Entomology', 'Mycology', 'Virology',
'Anatomical Sciences ', 'Public Health',
'Principles of Epidemiology', 'Technical English Language',
'General Knowledge','GPA']])
for name in columns:
dt[str(name)] = pd.qcut(dt[str(name)] ,20, labels=False, duplicates='drop')
dt[['Physiology', 'Biochemistry', 'Bacteriology',
'Parasitology and Entomology', 'Mycology', 'Virology',
'Anatomical Sciences ', 'Public Health',
'Principles of Epidemiology', 'Technical English Language',
'General Knowledge','GPA']]= ss_train.fit_transform(dt[['Physiology', 'Biochemistry', 'Bacteriology',
'Parasitology and Entomology', 'Mycology', 'Virology',
'Anatomical Sciences ', 'Public Health',
'Principles of Epidemiology', 'Technical English Language',
'General Knowledge','GPA']])
dt[['Physiology', 'Biochemistry', 'Bacteriology',
'Parasitology and Entomology', 'Mycology', 'Virology',
'Anatomical Sciences ', 'Public Health',
'Principles of Epidemiology', 'Technical English Language',
'General Knowledge','GPA']]= dt[['Physiology', 'Biochemistry', 'Bacteriology',
'Parasitology and Entomology', 'Mycology', 'Virology',
'Anatomical Sciences ', 'Public Health',
'Principles of Epidemiology', 'Technical English Language',
'General Knowledge','GPA']].round(3)
#--------------------------------------------------Generate test and train
print("6. Generate test and train ")
dt_train, dt_test = model_selection.train_test_split(dt, test_size=0.33, random_state=42, shuffle=True, stratify=dt["Status in the CMBSE"])
X_train = dt_train[['Anatomical Sciences ','Physiology','Biochemistry','Technical English Language','Bacteriology','Virology','Parasitology and Entomology','Mycology','Principles of Epidemiology','Public Health','General Knowledge','GPA','Gender', 'Residency Status', 'Entrance Semester', 'Type of Admission']]#,'No of Attemp']]
X_test= dt_test[['Anatomical Sciences ','Physiology','Biochemistry','Technical English Language','Bacteriology','Virology','Parasitology and Entomology','Mycology','Principles of Epidemiology','Public Health','General Knowledge','GPA','Gender', 'Residency Status', 'Entrance Semester', 'Type of Admission']]#,'No of Attemp']]
Y_train=dt_train[['Status in the CMBSE']]
Y_test=dt_test[['Status in the CMBSE']]
X_train2 = pd.DataFrame(X_train)
X_train2.to_csv(r'X_train.csv')
Y_train2 = pd.DataFrame(Y_train)
Y_train2.to_csv(r'Y_train.csv')
X_test2 = pd.DataFrame(X_test)
X_test2.to_csv(r'X_test.csv')
Y_test2 = pd.DataFrame(Y_test)
Y_test2.to_csv(r'Y_test.csv')
#--------------------------------------------------Resampling
print("7. ReSmoting")
from imblearn.combine import SMOTETomek
from imblearn.over_sampling import SMOTE
from imblearn.over_sampling import SVMSMOTE
sm= SMOTETomek(random_state=1)
X_train,Y_train = sm.fit_resample(X_train,Y_train)
X_train2 = pd.DataFrame(X_train)
X_train2.to_csv(r'X_train_transform.csv')
Y_train2 = pd.DataFrame(Y_train)
Y_train2.to_csv(r'Y_train_transform.csv')
#-------------------------------------------------Modeling
print("8. Modeling")
models2=[]
models=[]
results=[]
names=[]
Refit='roc_auc'
Scoring = [ 'roc_auc','precision','recall','accuracy','f1']
#-------------------------------------------------K-fold cross validation
print("9. K-fold cross validation")
from sklearn.model_selection import RepeatedStratifiedKFold
CV=RepeatedStratifiedKFold(n_splits=5,random_state=12,n_repeats=1)
print("10. LR")
print("\n LR",file=sourceFile)
random_grid={ }
clf = GridSearchCV(LogisticRegression(), random_grid, refit=Refit, scoring = Scoring, cv = CV, verbose=True, n_jobs=-1)
t0 = time.time()
lr=clf.fit(X_train, Y_train)
kr_fit = time.time() - t0
print("complexity and bandwidth selected and model fitted in %.3f s" % kr_fit , file=sourceFile)
models.append(("LR",lr.best_estimator_))
models2.append(("LR",lr.best_estimator_))
print("score on test: " + str(lr.score(X_test, Y_test)), file=sourceFile)
print("score on train: "+ str(lr.score(X_train, Y_train)), file=sourceFile)
print('parametrs:'+ str(lr.best_estimator_.get_params()), file=sourceFile)
avaluate(lr.best_estimator_)
# from sklearn.tree import DecisionTreeClassifier
# print("DT")
# print("\n DT", file=sourceFile)
# random_grid={}
# clf = GridSearchCV( DecisionTreeClassifier(), random_grid, refit=Refit, scoring = Scoring, cv = CV, verbose=True, n_jobs=-1)
# t0 = time.time()
# lr=clf.fit(X_train, Y_train)
# kr_fit = time.time() - t0
# print("complexity and bandwidth selected and model fitted in %.3f s" % kr_fit , file=sourceFile)
# models.append(("DT",lr.best_estimator_))
# models2.append(("DT",lr.best_estimator_))
# print("score on test: " + str(lr.score(X_test, Y_test)), file=sourceFile)
# print("score on train: "+ str(lr.score(X_train, Y_train)), file=sourceFile)
# print('parametrs:'+ str(lr.best_estimator_.get_params()), file=sourceFile)
# avaluate(lr.best_estimator_)
# from sklearn.naive_bayes import GaussianNB
# print("Naive Bayes")
# print("\n Naive Bayes", file=sourceFile)
# random_grid={}
# clf = GridSearchCV( DecisionTreeClassifier(), random_grid, refit=Refit, scoring = Scoring, cv = CV, verbose=True, n_jobs=-1)
# t0 = time.time()
# lr=clf.fit(X_train, Y_train)
# kr_fit = time.time() - t0
# print("complexity and bandwidth selected and model fitted in %.3f s" % kr_fit , file=sourceFile)
# models.append(("Naive Bayes",lr.best_estimator_))
# models2.append(("Naive Bayes",lr.best_estimator_))
# print("score on test: " + str(lr.score(X_test, Y_test)), file=sourceFile)
# print("score on train: "+ str(lr.score(X_train, Y_train)), file=sourceFile)
# print('parametrs:'+ str(lr.best_estimator_.get_params()), file=sourceFile)
# avaluate(lr.best_estimator_)
print("11. SVM")
print("\n SVM", file=sourceFile)
random_grid={}
clf = GridSearchCV( svm.SVC(probability=True), random_grid, refit=Refit, scoring = Scoring, cv = CV, verbose=True, n_jobs=-1)
t0 = time.time()
lr=clf.fit(X_train, Y_train)
kr_fit = time.time() - t0
print("complexity and bandwidth selected and model fitted in %.3f s" % kr_fit , file=sourceFile)
models.append(("SVM",lr.best_estimator_))
models2.append(("SVM",lr.best_estimator_))
print("score on test: " + str(lr.score(X_test, Y_test)), file=sourceFile)
print("score on train: "+ str(lr.score(X_train, Y_train)), file=sourceFile)
print('parametrs:'+ str(lr.best_estimator_.get_params()), file=sourceFile)
avaluate(lr.best_estimator_)
print("12. KNN")
print("\n KNN",file=sourceFile)
random_grid={
#'n_neighbors': [ 5,10],
#'weights': ['distance'],
#'leaf_size': [15]
}
clf = GridSearchCV( KNeighborsClassifier(), random_grid, refit=Refit, scoring = Scoring, cv = CV, verbose=True, n_jobs=-1)
t0 = time.time()
lr=clf.fit(X_train, Y_train)
kr_fit = time.time() - t0
print("complexity and bandwidth selected and model fitted in %.3f s" % kr_fit , file=sourceFile)
models.append(("KNN",lr.best_estimator_))
models2.append(("KNN",lr.best_estimator_))
print("score on test: " + str(lr.score(X_test, Y_test)), file=sourceFile)
print("score on train: "+ str(lr.score(X_train, Y_train)), file=sourceFile)
print('parametrs:'+ str(lr.best_estimator_.get_params()), file=sourceFile)
avaluate(lr.best_estimator_)
# print("HardVoting")
# print("\n HardVoting",file=sourceFile)
# from sklearn.ensemble import VotingClassifier
# final_model = VotingClassifier( estimators=[svm.SVC(probability=True),LogisticRegression(),KNeighborsClassifier()], voting='hard',n_jobs=-1)
# # training all the model on the train dataset
# lr=lr.fit(X_train, Y_train)
# models.append(("HardVoting",lr.best_estimator_))
# models2.append(("HardVoting",lr.best_estimator_))
# print("score on test: " + str(lr.score(X_test, Y_test)), file=sourceFile)
# print("score on train: "+ str(lr.score(X_train, Y_train)), file=sourceFile)
# print('parametrs:'+ str(lr.best_estimator_.get_params()), file=sourceFile)
# avaluate(lr.best_estimator_)
print("13. SoftVoting")
print("\n Voting",file=sourceFile)
from sklearn.ensemble import VotingClassifier
final_model = VotingClassifier( estimators=[svm.SVC(probability=True),LogisticRegression(),KNeighborsClassifier()], voting='hard',n_jobs=-1)
# training all the model on the train dataset
lr=lr.fit(X_train, Y_train)
models.append(("Voting",lr.best_estimator_))
models2.append(("Voting",lr.best_estimator_))
print("score on test: " + str(lr.score(X_test, Y_test)), file=sourceFile)
print("score on train: "+ str(lr.score(X_train, Y_train)), file=sourceFile)
print('parametrs:'+ str(lr.best_estimator_.get_params()), file=sourceFile)
avaluate(lr.best_estimator_)
print("14. RF")
print("\n RF",file=sourceFile)
#from sklearn.ensemble import RandomForestRegressor
random_grid={
'n_estimators': [ 10000],
#'max_depth' : [5,10,15,20],
'criterion' :[ 'entropy'],
#'class_weight' : [ 'balanced', 'None'],
#'max_depth': [10, 20, None],
# 'max_features': ['auto', 'sqrt'],
# 'min_samples_leaf': [1, 2, 4],
# 'min_samples_split': [2, 5, 10],
#'ccp_alpha': [0.1],
'bootstrap' : [True],
'oob_score': [False]
}
clf = GridSearchCV(RandomForestClassifier( n_jobs=-1, random_state=1), random_grid, refit=Refit, scoring =Scoring, cv = CV, verbose=True, n_jobs=-1)
t0 = time.time()
lr=clf.fit(X_train, Y_train)
kr_fit = time.time() - t0
print("complexity and bandwidth selected and model fitted in %.3f s" % kr_fit , file=sourceFile)
models.append(("RF",lr.best_estimator_))
models2.append(("RF",lr.best_estimator_))
print("score on test: " + str(lr.score(X_test, Y_test)), file=sourceFile)
print("score on train: "+ str(lr.score(X_train, Y_train)), file=sourceFile)
print('parametrs:'+ str(lr.best_estimator_.get_params()), file=sourceFile)
avaluate(lr.best_estimator_)
print("15. Bagging")
print("\n BAgging",file=sourceFile)
import xgboost as xgb
from sklearn.ensemble import BaggingRegressor
random_grid={
#"n_estimators" : [5000],
#'base_estimator' : [ LogisticRegression()],
#max_depth = [5, 10, 15, 25, 30]
#"max_samples" : [5],
#"max_features" : [ 15]
}
clf = GridSearchCV(BaggingClassifier(), random_grid, refit=Refit, scoring =Scoring, cv = CV, verbose=True, n_jobs=-1)
t0 = time.time()
lr=clf.fit(X_train, Y_train)
kr_fit = time.time() - t0
print("complexity and bandwidth selected and model fitted in %.3f s" % kr_fit , file=sourceFile)
models.append(("BG",lr.best_estimator_))
models2.append(("BG",lr.best_estimator_))
print("score on test: " + str(lr.score(X_test, Y_test)), file=sourceFile)
print("score on train: "+ str(lr.score(X_train, Y_train)), file=sourceFile)
print('parametrs:'+ str(lr.best_estimator_.get_params()), file=sourceFile)
avaluate(lr.best_estimator_)
print("16. ADA")
print("\n ADA",file=sourceFile)
random_grid={
#'n_estimators' : [1000],
#'base_estimator' : [ LogisticRegression()],
#'learning_rate': [0.01],
#'random_state' : [42 ] ,
#'algorithm': [ 'SAMME.R']
}
clf = GridSearchCV(AdaBoostClassifier(), random_grid, refit=Refit, scoring =Scoring, cv = CV, verbose=True, n_jobs=-1)
t0 = time.time()
lr=clf.fit(X_train, Y_train)
kr_fit = time.time() - t0
print("complexity and bandwidth selected and model fitted in %.3f s" % kr_fit , file=sourceFile)
models.append(("ADA",lr.best_estimator_))
models2.append(("ADA",lr.best_estimator_))
print("score on test: " + str(lr.score(X_test, Y_test)), file=sourceFile)
print("score on train: "+ str(lr.score(X_train, Y_train)), file=sourceFile)
print('parametrs:'+ str(lr.best_estimator_.get_params()), file=sourceFile)
avaluate(lr.best_estimator_)
print("17. XGB")
print("\n XGB",file=sourceFile)
from sklearn.ensemble import HistGradientBoostingClassifier
random_grid={
#'max_depth': [ 3, 18, 1],
#'gamma': [ 1,10],
#'reg_alpha' : [40,180,1],
#'reg_lambda' : [ 0,1],
#'colsample_bytree' : [ 0.5,1],
#'min_child_weight' : [ 0, 10, 1],
#'n_estimators': [10000],
#'seed':[ 0],
#'objective':['binary:logistic']
'max_leaf_nodes' : [10, 20, 30,]
}
#clf = GridSearchCV(GradientBoostingClassifier(learning_rate=0.01,max_features='sqrt',subsample=0.8,random_state=10,n_estimators=1000), random_grid, refit=Refit, scoring =Scoring, cv = CV, verbose=True, n_jobs=-1)
clf = GridSearchCV(HistGradientBoostingClassifier(learning_rate=0.01,random_state=42), random_grid, refit=Refit, scoring =Scoring, cv = CV, verbose=True, n_jobs=-1)
t0 = time.time()
lr=clf.fit(X_train, Y_train)
kr_fit = time.time() - t0
print("complexity and bandwidth selected and model fitted in %.3f s" % kr_fit , file=sourceFile)
models.append(("XGB",lr.best_estimator_))
models2.append(("XGB",lr.best_estimator_))
print("score on test: " + str(lr.score(X_test, Y_test)), file=sourceFile)
print("score on train: "+ str(lr.score(X_train, Y_train)), file=sourceFile)
print('parametrs:'+ str(lr.best_estimator_.get_params()), file=sourceFile)
avaluate(lr.best_estimator_)
# from xgboost import XGBClassifier
# print("XGB")
# print("\n XGB",file=sourceFile)
# random_grid={
# #'max_depth': [ 3, 18, 1],
# #'gamma': [ 1,10],
# #'reg_alpha' : [40,180,1],
# #'reg_lambda' : [ 0,1],
# #'colsample_bytree' : [ 0.5,1],
# #'min_child_weight' : [ 0, 10, 1],
# #'n_estimators': [10000],
# #'seed':[ 0],
# #'objective':['binary:logistic']
# }
# clf = GridSearchCV(GradientBoostingClassifier(learning_rate=0.01,max_features='sqrt',random_state=12,n_estimators=5000), random_grid, refit=Refit, scoring =Scoring, cv = CV, verbose=True, n_jobs=-1)
# #clf = GridSearchCV(XGBClassifier(), random_grid, refit=Refit, scoring =Scoring, cv = CV, verbose=True, n_jobs=-1)
# t0 = time.time()
# lr=clf.fit(X_train, Y_train)
# kr_fit = time.time() - t0
# print("complexity and bandwidth selected and model fitted in %.3f s" % kr_fit , file=sourceFile)
# models.append(("XGB",lr.best_estimator_))
# models2.append(("XGB",lr.best_estimator_))
# print("score on test: " + str(lr.score(X_test, Y_test)), file=sourceFile)
# print("score on train: "+ str(lr.score(X_train, Y_train)), file=sourceFile)
# print('parametrs:'+ str(lr.best_estimator_.get_params()), file=sourceFile)
# avaluate(lr.best_estimator_)
from xgboost import XGBClassifier
print("18. stack")
print("\n stack",file=sourceFile)
random_grid={}
rfc = RandomForestClassifier( n_jobs=-1,criterion='entropy', random_state=1,n_estimators=10000,ccp_alpha=0.1, bootstrap=True, oob_score=False)
xgb = HistGradientBoostingClassifier(learning_rate=0.01,random_state=1)
lr = LogisticRegression()
ada=AdaBoostClassifier()
clf2 = [('rfc',rfc),('xgb',xgb),('ADA',ada)] #list of (str, estimator)
from sklearn.ensemble import StackingClassifier
stack_model = StackingClassifier( estimators = clf2,final_estimator = xgb)
clf = GridSearchCV(stack_model, random_grid, refit=Refit, scoring =Scoring, cv = CV, verbose=True, n_jobs=-1)
t0 = time.time()
lr=clf.fit(X_train, Y_train)
kr_fit = time.time() - t0
print("complexity and bandwidth selected and model fitted in %.3f s" % kr_fit , file=sourceFile)
models.append(("stack",lr.best_estimator_))
models2.append(("stack",lr.best_estimator_))
print("score on test: " + str(lr.score(X_test, Y_test)), file=sourceFile)
print("score on train: "+ str(lr.score(X_train, Y_train)), file=sourceFile)
print('parametrs:'+ str(lr.best_estimator_.get_params()), file=sourceFile)
avaluate(lr.best_estimator_)
#-------------------------------------------------------------- Evaluation
print("19. Evaluate", file=sourceFile)
save_output()
plot_confusion_matrix()
plot_Calibration()
plot_Precision_Recall()
plot_comparing_accurecy()
plot_roc()
sourceFile.close()