-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy patheval.py
More file actions
51 lines (32 loc) · 1.42 KB
/
eval.py
File metadata and controls
51 lines (32 loc) · 1.42 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
import matplotlib
matplotlib.use("Agg")
import matplotlib.pyplot as plt
import seaborn as sns
import numpy as np
from sklearn import metrics
def eval(ground_truths, prediction_scores, dir_to_print):
precisions, recalls, pr_thresholds = metrics.precision_recall_curve(ground_truths, prediction_scores)
precision_recall_area = metrics.auc(recalls, precisions)
fpr, tpr, roc_thresholds = metrics.roc_curve(ground_truths, prediction_scores, pos_label=1)
roc_area = metrics.auc(fpr, tpr)
max_accuracy = find_max_accuracy(ground_truths, prediction_scores, pr_thresholds)
printGraph(recalls, precisions, "precision vs recall", "recall", "precision", dir_to_print)
printGraph(fpr, tpr, "ROC curve", "False Positive Rate", "True Positive Rate", dir_to_print)
print "Max Accuracy: " + max_accuracy
print "ROC AUC: " + roc_area
print "PR AUC: " + precision_recall_area
def find_max_accuracy(ground_truths, prediction_scores, pr_thresholds):
mx = 0.0
for thresh in pr_thresholds:
acc = metrics.accuracy_score(ground_truths, prediction_scores > thresh)
if acc > mx: mx = acc
return mx
def printGraph(xs, ys, graph_name, xlbl, ylbl, dir_to_print):
sns.set_style("darkgrid")
plt.plot(xs, ys, 'r', label = "Test")
plt.legend(loc = "upper left")
plt.title(graph_name)
plt.xlabel(xlbl)
plt.ylabel(ylbl)
plt.savefig(dir_to_print + graph_name)
plt.clf()