-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcurves.py
155 lines (122 loc) · 4.13 KB
/
curves.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
from sklearn.linear_model import SGDClassifier
from sklearn.neural_network import MLPClassifier
from sklearn.tree import DecisionTreeClassifier
from sklearn.ensemble import RandomForestClassifier
from sklearn.ensemble import AdaBoostClassifier
from sklearn.neighbors import KNeighborsClassifier
from sklearn.naive_bayes import GaussianNB
from sklearn.svm import SVC
from sklearn.metrics import precision_score, recall_score
from sklearn.metrics import roc_auc_score, roc_curve
from sklearn.metrics import precision_recall_curve
from sklearn.model_selection import cross_val_predict
import matplotlib.pyplot as plt
import numpy as np
class PlotCurves():
def __init__(self, y_preds, X_train, y_train):
self.y_preds = y_preds
self.X_train = X_train
self.y_train = y_train
self.plot_style = styles = {
'SGD': 'r--',
'MLP': 'm--',
'Decision Tree': 'y--',
'Random Forest': 'g--',
'AdaBoost': 'b--',
'KNN': 'k--',
'NB': 'p--',
'SVM': 'c--'
}
# define classifier
sgd_clf = SGDClassifier(random_state=42, max_iter=100)
mlp_clf = MLPClassifier(hidden_layer_sizes=(16,))
tree_clf = DecisionTreeClassifier()
forest_clf = RandomForestClassifier()
adaboost_clf = AdaBoostClassifier()
knn_clf = KNeighborsClassifier()
nb_clf = GaussianNB()
svm_clf = SVC()
self.clf = {
'SGD': sgd_clf,
'MLP': mlp_clf,
'Decision Tree': tree_clf,
'Random Forest': forest_clf,
'AdaBoost': adaboost_clf,
'KNN': knn_clf,
'NB': nb_clf,
'SVM': svm_clf
}
def precision_vs_recall(self, clf_list, thr):
if clf_list == 'all':
clf_list = self.clf.keys()
# plot precision versus recall curve for each classifier
for clf in clf_list:
# compute decision scores using cross valuation
y_scores = self.compute_scores(clf)
precisions, recalls, thresholds = precision_recall_curve(self.y_train, y_scores)
# plot the precision and recall curves
plt.plot(recalls, precisions, self.plot_style[clf], label=clf)
if thr == 'default':
thr = 0
if thr == 'best':
thr= thresholds[np.argmax((recalls <= 0.9))]
# higlight threshold
y_pred_thr = y_scores >= thr
hl_precision = precision_score(self.y_train, y_pred_thr)
hl_recall = recall_score(self.y_train, y_pred_thr)
plt.plot([0, hl_recall], [hl_precision, hl_precision], 'r:')
plt.plot([hl_recall, hl_recall], [0, hl_precision], 'r:')
plt.plot([hl_recall], [hl_precision], 'ro')
# style plot
plt.grid(True)
plt.xlabel('Recall')
plt.ylabel('Precision')
plt.legend()
plt.show()
def roc_curve(self, clf_list, thr):
if clf_list == 'all':
clf_list = self.clf.keys()
for clf in clf_list:
# compute decision scores using cross valuation
y_scores = self.compute_scores(clf)
# fpr = false positive rate = recall/sensitivity
# tpr = true positive rate
fpr, tpr, thresholds = roc_curve(self.y_train, y_scores)
# plot the roc curve
plt.plot(fpr, tpr, self.plot_style[clf], label=clf)
if thr == 'default':
thr = 0
if thr == 'best':
thr = thresholds[np.argmax((recalls <= 0.9))]
y_pred = y_scores >= thr
fp = np.sum(np.logical_and(y_pred == True, self.y_train == 0))
tp = np.sum(np.logical_and(y_pred == True, self.y_train == 1))
fpr = fp / np.sum(self.y_train == 0)
tpr = tp / np.sum(self.y_train == 1)
plt.plot([0, fpr], [tpr, tpr], 'r:')
plt.plot([fpr, fpr], [0, tpr], 'r:')
plt.plot([fpr], [tpr], 'ro')
# style plot
plt.grid(True)
plt.xlabel('Specificity')
plt.ylabel('Recall/sensitivity')
plt.legend()
plt.show()
def roc_auc_score(self, clf_list):
if clf_list == 'all':
clf_list = self.clf.keys()
print("ROC AUC scores:")
for clf in clf_list:
y_scores = self.compute_scores(clf)
roc_auc = roc_auc_score(self.y_train, y_scores)
print("{}: {}".format(clf, roc_auc))
def compute_scores(self, classifier):
classifier = self.clf[classifier]
method = 'decision_function'
if not hasattr(classifier, 'decision_function') and hasattr(classifier, 'predict_proba'):
method = 'predict_proba'
y_scores = cross_val_predict(classifier, self.X_train, self.y_train,
cv=3, method=method)
if method == 'predict_proba':
y_scores = y_scores[:,1]
return y_scores