-
Notifications
You must be signed in to change notification settings - Fork 0
/
plot_roc_curve.py
157 lines (118 loc) · 4.67 KB
/
plot_roc_curve.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
import numpy as np
import matplotlib as mpl
import matplotlib.pyplot as plt
from sklearn.base import is_classifier
from sklearn.metrics import roc_curve, auc
from sklearn.model_selection import StratifiedKFold
from sklearn.metrics import roc_auc_score
from sklearn.model_selection import check_cv
import pandas as pd
def filter_idxs(v_array, idxs, return_copy=False, convert_to_ndarray=True):
if convert_to_ndarray:
if isinstance(v_array, pd.DataFrame):
if return_copy:
return v_array.iloc[idxs].copy().values
else:
return v_array.iloc[idxs].values
elif isinstance(v_array, np.ndarray):
if return_copy:
return v_array[idxs].copy()
else:
return v_array[idxs]
else:
if isinstance(v_array, pd.DataFrame):
if return_copy:
return v_array.iloc[idxs].copy()
else:
return v_array.iloc[idxs]
elif isinstance(v_array, np.ndarray):
if return_copy:
return v_array[idxs].copy()
else:
return v_array[idxs]
def plot_roc_curve(X, y,
clf,
clf_label=None,
target_column=1, # for binary is the usual
class_name=None,
color_mean_roc_curve='r',
cv=5,
n_points_roc_curve=200,
show_fold_curves=True, show_fold_scores=False, plot_chance_curve=True,
figsize=None, fig_dpi=None,
override_label=None,
dict_pyplot_style=None,
fig=None,
return_fig=False):
assert is_classifier(clf), 'clf must be a classifier.'
if clf_label is None:
clf_label = ''
if not figsize:
figsize = (12, 7)
kfold = check_cv(cv=cv, y=y, classifier=True)
score_acc = list()
# From:
# https://scikit-learn.org/stable/auto_examples/model_selection/plot_roc_crossval.html#:~:text=Example%20of%20Receiver%20Operating%20Characteristic,rate%20on%20the%20X%20axis.
tprs = []
aucs = []
mean_fpr = np.linspace(0, 1, n_points_roc_curve)
with mpl.rc_context(dict_pyplot_style):
if fig is None:
fig = plt.figure(figsize=figsize, dpi=fig_dpi)
for ifold, (train_index, test_index) in enumerate(kfold.split(X, y)):
X_train = filter_idxs(X, train_index, return_copy=True) # fazer uma copia aqui porque a ideia é poder alterar X_train nessa iteração
y_train = filter_idxs(y, train_index)
X_test = filter_idxs(X, test_index)
y_test = filter_idxs(y, test_index)
if isinstance(y_train, np.ndarray):
y_train = y_train.ravel()
y_test = y_test.ravel()
# I can process X_train here (standardization for example...)
#
#
# Now I'll train
clf.fit(X_train, y_train)
y_true = y_test
y_pred = clf.predict(X_test)
y_pred_proba = clf.predict_proba(X_test)[:, target_column]
# Roc curve
fpr, tpr, _ = roc_curve(y_true, y_pred_proba)
interp_tpr = np.interp(mean_fpr, fpr, tpr)
interp_tpr[0] = 0.0
tprs.append(interp_tpr)
auc_ = roc_auc_score(y_true, y_pred_proba)
if show_fold_curves:
if show_fold_scores:
plt.plot(fpr, tpr, '-', c='gray', label=f'AUC fold {ifold+1}: {auc_:.2f}', figure=fig)
else:
plt.plot(fpr, tpr, '-', c='gray', figure=fig)
aucs.append(auc_)
mean_tpr = np.mean(tprs, axis=0)
mean_tpr[-1] = 1.0
mean_auc = auc(mean_fpr, mean_tpr)
std_auc = np.std(aucs)
if override_label:
model_label = override_label
else:
if class_name:
model_label = r"Mean ROC %s for class %s, AUC = %0.2f $\pm$ %0.2f" % (clf_label, class_name, mean_auc, std_auc)
else:
model_label = r"Mean ROC %s, AUC = %0.2f $\pm$ %0.2f" % (clf_label, mean_auc, std_auc)
plt.plot(
mean_fpr,
mean_tpr,
color=color_mean_roc_curve,
label=model_label,
lw=4,
alpha=0.8,
figure=fig
)
if plot_chance_curve:
plt.plot([0, 1], [0, 1], '--', label='chance', lw=4, figure=fig)
plt.legend()
plt.xlabel('False positive rate')
plt.ylabel('True positive rate')
if return_fig:
return fig
else:
plt.show()