-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathfunctions_statistical_performance.py
More file actions
339 lines (262 loc) · 13.8 KB
/
functions_statistical_performance.py
File metadata and controls
339 lines (262 loc) · 13.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
from sklearn.model_selection import train_test_split
from sklearn.metrics import accuracy_score
from sklearn.feature_selection import VarianceThreshold
from sklearn.feature_selection import RFE
from sklearn.ensemble import RandomForestClassifier
from sklearn.metrics import precision_score, recall_score, roc_auc_score, roc_curve, average_precision_score, precision_recall_curve
from scipy import interp
from sklearn.model_selection import cross_val_score
from sklearn.metrics import classification_report
from sklearn.model_selection import RandomizedSearchCV
from sklearn.preprocessing import LabelBinarizer
from sklearn.model_selection import cross_val_predict
from sklearn.metrics import confusion_matrix
from sklearn.model_selection import StratifiedKFold
from imblearn.over_sampling import SMOTE
import operator
#AUC ROC Curve Scoring Function for Multi-class Classification
#"macro"
#"weighted"
# None
def multiclass_roc_auc_score(y_test, y_probs, average="macro"):
lb = LabelBinarizer()
lb.fit(y_test)
y_test = lb.transform(y_test)
return roc_auc_score(y_test, y_probs, average=average)
def multiclass_average_precision_score(y_test, y_probs, average="macro"):
lb = LabelBinarizer()
lb.fit(y_test)
y_test = lb.transform(y_test)
return average_precision_score(y_test, y_probs, average=average)
def multiclass_roc_curve(y_test, y_probs):
lb = LabelBinarizer()
lb.fit(y_test)
y_test = lb.transform(y_test)
fpr = dict()
tpr = dict()
for i in range(y_probs.shape[1]):
fpr[i], tpr[i], _ = roc_curve(y_test[:, i], y_probs[:, i])
return (fpr, tpr)
def multiclass_average_precision_curve(y_test, y_probs):
lb = LabelBinarizer()
lb.fit(y_test)
y_test = lb.transform(y_test)
precision = dict()
recall = dict()
for i in range(y_probs.shape[1]):
precision[i], recall[i], _ = precision_recall_curve(y_test[:, i], y_probs[:, i])
return (precision, recall)
def Accuracykfold(X,y,splits):
Xs = np.copy(X)
ys=np.copy(y)
numfolds=5;
numlabels=4;
performancesAccuracy=np.empty([numfolds, 1]);
index=0
for train, test in splits:
# print("%s %s" % (train, test))
clf = RandomForestClassifier(n_estimators = 200, max_features='sqrt', max_depth=420,random_state=0)
#{'n_estimators': 200, 'max_features': 'sqrt', 'max_depth': 420}
clf.fit(Xs[train,:], ys[train])
# Predicting the Test set results
y_pred = clf.predict(Xs[test,:])
performancesAccuracy[index]=accuracy_score(ys[test], y_pred)
index+=1
return performancesAccuracy
# returns performances and splits/models used in the cross-validation
def AUCAUPkfold(X,y,smoteflag,verbose=True):
numfolds=5;
numlabels=4;
cv = StratifiedKFold(n_splits=numfolds, shuffle=True)
Xs = np.copy(X)
ys=np.copy(y)
if smoteflag==True:
smote=SMOTE()
Xs, ys= smote.fit_sample(Xs, ys)
performancesAUC=np.empty([numfolds, numlabels]);
performancesAUP=np.empty([numfolds, numlabels]);
splits=[];
model_per_fold=[]
index=0
for train, test in cv.split(Xs, ys):
# print("%s %s" % (train, test))
clf = RandomForestClassifier(n_estimators = 200, max_features='sqrt', max_depth=420, random_state=0)
#clf = RandomForestClassifier(n_estimators = 1800, max_features='sqrt', max_depth=260)
#{'n_estimators': 200, 'max_features': 'sqrt', 'max_depth': 420}
splits.append([train, test])
#print(Xs[train,:].shape)
#print(ys[train].shape)
clf.fit(Xs[train,:], ys[train])
# Predicting the Test set results
y_pred = clf.predict(Xs[test,:])
y_probs= clf.predict_proba(Xs[test,:])
performancesAUC[index,:]=np.array(multiclass_roc_auc_score(ys[test], y_probs, average=None))
performancesAUP[index,:]=np.array(multiclass_average_precision_score(ys[test], y_probs, average=None))
index+=1
model_per_fold.append(clf)
#if verbose==True:
# print(multiclass_roc_auc_score(ys[test], y_probs, average=None))
if verbose==True:
print("AUC: average over the folds")
print(performancesAUC.mean(axis=0))
print("AUC: std over the folds")
print(performancesAUC.std(axis=0))
if verbose==True:
print("AUP: average over the folds")
print(performancesAUP.mean(axis=0))
print("AUP: std over the folds")
print(performancesAUP.std(axis=0))
return (performancesAUC, performancesAUP, splits, model_per_fold)
def ROCkfold(X,y,splits,verbose=True):
Xs = np.copy(X)
ys=np.copy(y)
numfolds=5;
numlabels=4;
mean_fpr = np.linspace(0, 1, 500)
performancesAUC=np.empty([numfolds, numlabels]);
performancesROC=[np.empty([numfolds,len(mean_fpr)]) for ind in range(numlabels)];
index=0
for train, test in splits:
# print("%s %s" % (train, test))
clf = RandomForestClassifier(n_estimators = 200, max_features='sqrt', max_depth=420,random_state=0)
#{'n_estimators': 200, 'max_features': 'sqrt', 'max_depth': 420}
clf.fit(Xs[train,:], ys[train])
# Predicting the Test set results
y_pred = clf.predict(Xs[test,:])
y_probs= clf.predict_proba(Xs[test,:])
# ROC curve for the classes--> returns two dictionaries
fprclasses, tprclasses=multiclass_roc_curve(ys[test], y_probs)
for c in range(numlabels):
interp_tpr = interp(mean_fpr, fprclasses[c], tprclasses[c])
interp_tpr[0] = 0.0
performancesROC[c][index, :]=interp_tpr
performancesAUC[index,:]=np.array(multiclass_roc_auc_score(ys[test], y_probs, average=None))
index+=1
#if verbose==True:
# print(multiclass_roc_auc_score(ys[test], y_probs, average=None))
mean_tpr =[np.empty([1,len(mean_fpr)]) for ind in range(numlabels)];
std_tpr =[np.empty([1,len(mean_fpr)]) for ind in range(numlabels)];
tprs_upper =[np.empty([1,len(mean_fpr)]) for ind in range(numlabels)];
tprs_lower =[np.empty([1,len(mean_fpr)]) for ind in range(numlabels)];
for c in range(numlabels):
mean_tpr[c]=np.mean(performancesROC[c], axis=0)
mean_tpr[c][-1] = 1.0
std_tpr[c]=np.std(performancesROC[c], axis=0)
tprs_upper[c]=np.minimum(mean_tpr[c] + std_tpr[c], 1)
tprs_lower[c]=np.maximum(mean_tpr[c] - std_tpr[c], 0)
if verbose==True:
print("AUC: average over the folds")
print(performancesAUC.mean(axis=0))
print("AUC: std over the folds")
print(performancesAUC.std(axis=0))
return performancesAUC, performancesROC, mean_fpr, mean_tpr, std_tpr, tprs_upper, tprs_lower
def ROCplot(mean_fpr, mean_tpr, std_tpr, tprs_upper, tprs_lower, performancesAUC, labelc):
mean_auc=performancesAUC.mean(axis=0)
std_auc=performancesAUC.std(axis=0)
fig, ax = plt.subplots()
ax.plot([0, 1], [0, 1], linestyle='--', lw=2, color='r', label='Chance', alpha=.8)
ax.plot(mean_fpr, mean_tpr, color='b',label=r'Mean ROC (AUC = %0.2f $\pm$ %0.2f)' % (mean_auc, std_auc), lw=2, alpha=.8)
ax.fill_between(mean_fpr, tprs_lower, tprs_upper, color='grey', alpha=.2, label=r'$\pm$ 1 std. dev.')
ax.set(xlim=[-0.05, 1.05], ylim=[-0.05, 1.05], title="ROC "+"Class "+ str(labelc+1))
ax.legend(loc="lower right")
plt.xlabel('False Positive Rate')
plt.ylabel('True Positive Rate')
plt.show()
# labeld--> label for all the dbs
def ROCMultiPlot(mean_fprL, mean_tprL, std_tprL, tprs_upperL, tprs_lowerL, performancesAUCL, labelc, labeld, colord):
mean_auc=[p.mean(axis=0) for p in performancesAUCL]
std_auc=[p.std(axis=0) for p in performancesAUCL]
fig, ax = plt.subplots(figsize=(5, 5))
#ax.plot([0, 1], [0, 1], linestyle='--', lw=2, color='r', label='Chance', alpha=.8)
for d in range(len(labeld)):
ax.plot(mean_fprL[d], mean_tprL[d], color=colord[d],label=labeld[d]+ r': AUC = %0.4f $\pm$ %0.4f' % (mean_auc[d], std_auc[d]), lw=2, alpha=.5)
ax.fill_between(mean_fprL[d], tprs_lowerL[d], tprs_upperL[d], color=colord[d], alpha=.1)
ax.set(xlim=[0, 1], ylim=[0, 1], title="ROC "+"Class "+ str(labelc+1))
ax.legend(loc="lower right")
plt.xlabel('False Positive Rate')
plt.ylabel('True Positive Rate')
plt.show()
def ROCMultiPlotCallable(mean_fprL, mean_tprL, std_tprL, tprs_upperL, tprs_lowerL, performancesAUCL, labelc, labeld, colord,ax):
mean_auc=[p.mean(axis=0) for p in performancesAUCL]
std_auc=[p.std(axis=0) for p in performancesAUCL]
#ax.plot([0, 1], [0, 1], linestyle='--', lw=2, color='r', label='Chance', alpha=.8)
for d in range(len(labeld)):
ax.plot(mean_fprL[d], mean_tprL[d], color=colord[d],label=labeld[d]+ r': AUC = %0.4f $\pm$ %0.4f' % (mean_auc[d], std_auc[d]), lw=2, alpha=.5)
ax.fill_between(mean_fprL[d], tprs_lowerL[d], tprs_upperL[d], color=colord[d], alpha=.1)
ax.legend(loc="lower right")
def PrecisionRecallCurvekfold(X,y,splits,verbose=True):
Xs = np.copy(X)
ys=np.copy(y)
numfolds=5;
numlabels=4;
mean_recall = np.linspace(1, 0, 500)
performancesAUP=np.empty([numfolds, numlabels]);
performancesPrecisionRecall=[np.empty([numfolds,len(mean_recall)]) for ind in range(numlabels)];
index=0
for train, test in splits:
# print("%s %s" % (train, test))
clf = RandomForestClassifier(n_estimators = 200, max_features='sqrt', max_depth=420,random_state=0)
#clf = RandomForestClassifier(n_estimators = 1800, max_features='sqrt', max_depth=260)
#{'n_estimators': 200, 'max_features': 'sqrt', 'max_depth': 420}
clf.fit(Xs[train,:], ys[train])
# Predicting the Test set results
y_pred = clf.predict(Xs[test,:])
y_probs= clf.predict_proba(Xs[test,:])
# Precision-Recall curve for the classes--> returns two dictionaries
precisionclasses, recallclasses=multiclass_average_precision_curve(ys[test], y_probs)
for c in range(numlabels):
interp_precision = interp(mean_recall, recallclasses[c][::-1], precisionclasses[c][::-1])
performancesPrecisionRecall[c][index, :]=interp_precision
performancesAUP[index,:]=np.array(multiclass_average_precision_score(ys[test], y_probs, average=None))
index+=1
#if verbose==True:
# print(multiclass_average_precision_score(ys[test], y_probs, average=None))
mean_precision =[np.empty([1,len(mean_recall)]) for ind in range(numlabels)];
std_precision =[np.empty([1,len(mean_recall)]) for ind in range(numlabels)];
precision_upper =[np.empty([1,len(mean_recall)]) for ind in range(numlabels)];
precision_lower =[np.empty([1,len(mean_recall)]) for ind in range(numlabels)];
for c in range(numlabels):
mean_precision[c]=np.mean(performancesPrecisionRecall[c], axis=0)
std_precision[c]=np.std(performancesPrecisionRecall[c], axis=0)
precision_upper[c]=np.minimum(mean_precision[c] + std_precision[c], 1)
precision_lower[c]=np.maximum(mean_precision[c] - std_precision[c], 0)
if verbose==True:
print("AUP: average over the folds")
print(performancesAUP.mean(axis=0))
print("AUP: std over the folds")
print(performancesAUP.std(axis=0))
return performancesAUP, performancesPrecisionRecall, mean_recall, mean_precision, std_precision, precision_upper, precision_lower
def PrecisionRecallplot(mean_recall, mean_precision, std_precision, precision_upper, precision_lower, performancesAUP, labelc):
mean_aup=performancesAUP.mean(axis=0)
std_aup=performancesAUP.std(axis=0)
fig, ax = plt.subplots()
ax.plot(mean_recall, mean_precision, color='b',label=r'Mean Precision Recall (AUP = %0.2f $\pm$ %0.2f)' % (mean_aup, std_aup), lw=2, alpha=.8)
ax.fill_between(mean_recall, precision_lower, precision_upper, color='grey', alpha=.2, label=r'$\pm$ 1 std. dev.')
ax.set(xlim=[-0.05, 1.05], ylim=[-0.05, 1.05], title="Precision-Recall Curve "+ "Class "+ str(labelc +1))
ax.legend(loc="lower left")
plt.xlabel('Recall')
plt.ylabel('Precision')
plt.show()
# labeld--> label for all the dbs
def PrecisionRecallMultiPlot(mean_recallL, mean_precisionL, std_precisionL, precision_upperL, precision_lowerL, performancesAUPL, labelc, labeld, colord):
mean_aup=[p.mean(axis=0) for p in performancesAUPL]
std_aup=[p.std(axis=0) for p in performancesAUPL]
fig, ax = plt.subplots(figsize=(5, 5))
for d in range(len(labeld)):
ax.plot(mean_recallL[d], mean_precisionL[d], color=colord[d],label=labeld[d]+ r': AUP = %0.4f $\pm$ %0.4f' % (mean_aup[d], std_aup[d]), lw=2, alpha=.5)
ax.fill_between(mean_recallL[d], precision_lowerL[d], precision_upperL[d], color=colord[d], alpha=.1)
ax.set(xlim=[0, 1], ylim=[0, 1], title="Precision-Recall Curve "+ "Class "+ str(labelc +1))
ax.legend(loc="lower left")
plt.xlabel('Recall')
plt.ylabel('Precision')
plt.show()
def PrecisionRecallMultiPlotCallable(mean_recallL, mean_precisionL, std_precisionL, precision_upperL, precision_lowerL, performancesAUPL, labelc, labeld, colord,ax):
mean_aup=[p.mean(axis=0) for p in performancesAUPL]
std_aup=[p.std(axis=0) for p in performancesAUPL]
for d in range(len(labeld)):
ax.plot(mean_recallL[d], mean_precisionL[d], color=colord[d],label=labeld[d]+ r': AUP = %0.4f $\pm$ %0.4f' % (mean_aup[d], std_aup[d]), lw=2, alpha=.5)
ax.fill_between(mean_recallL[d], precision_lowerL[d], precision_upperL[d], color=colord[d], alpha=.1)
ax.legend(loc="lower left")