-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathModelEvaluation.py
166 lines (121 loc) · 5.64 KB
/
ModelEvaluation.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
##############################################
# (c) Copyright 2018-2019 Kenza Tazi and Thomas Zhu
# This software is distributed under the terms of the GNU General Public
# Licence version 3 (GPLv3)
##############################################
import matplotlib.pyplot as plt
from sklearn import metrics
import numpy as np
def get_accuracy(model, validation_data, validation_truth, para_num=21):
""" returns model accuracy """
validation_data = np.concatenate(validation_data)
validation_data = validation_data.reshape(-1, para_num)
validation_truth = np.concatenate(validation_truth)
validation_truth = validation_truth.reshape(-1, 2)
accuracy = model.evaluate(validation_data, validation_truth)
return accuracy[0]
def ROC(validation_predictions, validation_truth, bayes_mask=None,
emp_mask=None, name=None):
""" Plots Receiver Operating Characteristic (ROC) curve """
false_positive_rate, true_positive_rate, _ = metrics.roc_curve(
validation_truth[:, 0], validation_predictions[:, 0], pos_label=1)
if name is None:
plt.figure('ROC')
plt.title('ROC')
else:
plt.figure(name + ' ROC')
plt.title(name + ' ROC')
plt.plot(false_positive_rate, true_positive_rate, label='Model')
plt.xlabel('False positive rate')
plt.ylabel('True positive rate')
plt.plot([0, 1], [0, 1], c='darkgrey', linestyle='--', linewidth=0.75,
label='Random classifier')
if bayes_mask is not None:
validation_truth = validation_truth.astype(int)
bayes_mask = bayes_mask.astype(int)
tn, fp, fn, tp = (metrics.confusion_matrix(
validation_truth[:, 0], bayes_mask[:, 0], labels=(0, 1))).ravel()
# print(tn, fp, fn, tp)
plt.scatter(float(fp) / float(tn + fp), float(tp) /
float(fn + tp), marker='o', c='g', label='Bayesian mask')
if emp_mask is not None:
validation_truth = validation_truth.astype(int)
emp_mask = emp_mask.astype(int)
tn, fp, fn, tp = (metrics.confusion_matrix(
validation_truth[:, 0], emp_mask[:, 0], labels=(0, 1))).ravel()
# print(tn, fp, fn, tp)
plt.scatter(float(fp) / float(tn + fp), float(tp) /
float(fn + tp), marker='*', c='orange', label='Empirical mask')
plt.legend(fontsize='small')
def nROC(validation_predictions, validation_truths, colours, bayes_masks=None,
emp_masks=None, names=None, title=None):
"""
Plots multiple Receiver Operating Characteristic (ROC)
curve on the same graph.
Parameters
-----------
validation_predictions: n dimensionsal array
validation_truths: n dimensionsal array
colours: 1d array of strings
bayes_masks: n dimensionsal array
emp_masks= n dimensionsal array
names= 1d array of strings
Returns
---------
Matplotlib plot
"""
table = np.column_stack((validation_predictions, validation_truths,
bayes_masks, emp_masks, names, colours))
if title is not None:
plt.figure(title + ' ROC')
plt.title(title + ' ROC')
else:
plt.figure('ROC')
plt.title('ROC')
plt.xlabel('False positive rate')
plt.ylabel('True positive rate')
plt.plot([0, 1], [0, 1], c='darkgrey', linestyle='--', linewidth=0.75,
label='Random classifier')
for t in table:
validation_prediction, validation_truth, bayes_mask, emp_mask, name, colour = t
false_positive_rate, true_positive_rate, _ = metrics.roc_curve(
validation_truth[:, 0], validation_prediction[:, 0], pos_label=1)
plt.plot(false_positive_rate, true_positive_rate, c=colour, label='Model over ' + name)
if bayes_mask is not None:
validation_truth = validation_truth.astype(int)
bayes_mask = bayes_mask.astype(int)
tn, fp, fn, tp = (metrics.confusion_matrix(
validation_truth[:, 0], bayes_mask[:, 0], labels=(0, 1))).ravel()
# print(tn, fp, fn, tp)
plt.scatter(float(fp) / float(tn + fp), float(tp) / float(fn + tp),
marker='o', label='Bayesian mask over ' + name, c=colour)
if emp_mask is not None:
validation_truth = validation_truth.astype(int)
emp_mask = emp_mask.astype(int)
tn, fp, fn, tp = (metrics.confusion_matrix(
validation_truth[:, 0], emp_mask[:, 0], labels=(0, 1))).ravel()
# print(tn, fp, fn, tp)
plt.scatter(float(fp) / float(tn + fp), float(tp) / float(fn + tp),
marker='*', label='Empirical mask over ' + name, c=colour)
plt.legend(fontsize='small')
def AUC(model, validation_data, validation_truth):
"""Returs area under Receiver Operating Characteristic (ROC) curve"""
predictions = model.predict(validation_data)
auc = metrics.roc_auc_score(validation_truth[:, 1], predictions[:, 1])
return auc
def precision_vs_recall(model, validation_data, validation_truth):
"""Plots precision vs recall curve"""
predictions = np.nan_to_num(model.predict(validation_data))
precision, recall, _ = metrics.precision_recall_curve(
validation_truth[:, 0], predictions[:, 0], pos_label=1)
plt.figure('Precision vs recall curve')
plt.title('Precision vs recall curve')
plt.plot(precision, recall)
plt.xlabel('Precision')
plt.ylabel('Recall')
def confusion_matrix(model, validation_data, validation_truth):
""" Returns a confusion matrix"""
predictions = model.predict_label(validation_data)
m = metrics.confusion_matrix(
validation_truth[:, 0], predictions, labels=(0, 1))
return m