-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathevaluation.py
143 lines (124 loc) · 4.81 KB
/
evaluation.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
"""Evaluation functions for LatticeRNN."""
import matplotlib
matplotlib.use('Agg')
import matplotlib as mpl
import matplotlib.pyplot as plt
import numpy as np
import os
import sys
from sklearn.metrics import roc_curve, precision_recall_curve, auc
def log(array):
"""Clip the elements of arrays before taking log."""
return np.log(np.clip(array, a_min=1e-8, a_max=1.0))
def nce(labels, predictions):
"""Calculate normalised cross entropy.
Arguments:
labels {array} -- numpy array of labels {0, 1}
predictions {array} -- numpy array of predictions, [0, 1]
Returns:
float -- NCE score
"""
assert len(labels) == len(predictions), \
"dimension of labels is not the same as predictions"
percentage_correct = np.sum(labels) / len(labels)
label_entropy = - percentage_correct * log(percentage_correct) \
- (1 - percentage_correct) * log(1 - percentage_correct)
conditional_entropy = (np.dot(labels, log(predictions)) \
+ np.dot((1 - labels), log(1 - predictions))) \
/ (-len(labels))
score = (label_entropy - conditional_entropy) / label_entropy
return score
def roc(labels, predictions):
"""Compute ROC curve and its AUC.
Arguments:
labels {array} -- numpy array of labels {0, 1}
predictions {array} -- numpy array of predictions, [0, 1]
Returns:
tuple -- fpr array, tpr array, area float
"""
fpr, tpr, _ = roc_curve(labels, predictions)
area = auc(fpr, tpr)
return fpr, tpr, area
def pr(labels, predictions):
"""Compute precision-recall curve and its AUC.
Arguments:
labels {array} -- numpy array of labels {0, 1}
predictions {array} -- numpy array of predictions, [0, 1]
Returns:
tuple -- precision array, recall array, area float
"""
precision, recall, _ = precision_recall_curve(labels, predictions)
area = auc(recall, precision)
return precision, recall, area
def plot_roc(fpr, tpr, area, name, dst_dir):
"""Plotting ROC curve.
Arguments:
tpr {list} -- a list of numpy 1D array for true positive rate
fpr {list} -- a list of numpy 1D array for false positive rate
area {list} -- a list of floats for area under curve
name {str} -- text for the legend
dst_dir {str} -- output figure directory, file name `roc.pdf`
"""
plt.clf()
plt.figure(figsize=(3, 3))
plt.rcParams["font.family"] = "Times New Roman"
for (x_val, y_val, a_val, string) in zip(fpr, tpr, area, name):
label = string + ' (AUC = %0.4f)' %a_val
plt.plot(x_val, y_val, label=label)
plt.legend(loc='lower right')
plt.plot([0, 1], [0, 1], 'r--')
plt.xlim([0, 1])
plt.ylim([0, 1])
plt.ylabel('True Positive Rate')
plt.xlabel('False Positive Rate')
plt.savefig(os.path.join(dst_dir, 'roc.pdf'), bbox_inches='tight')
plt.close()
def plot_pr(precision, recall, area, name, dst_dir=None):
"""Plotting ROC curve.
Arguments:
tpr {list} -- a list of numpy 1D array for true positive rate
fpr {list} -- a list of numpy 1D array for false positive rate
area {list} -- a list of floats for area under curve
name {str} -- text for the legend
dst_dir {str} -- output figure directory, file name `roc.pdf`
"""
if dst_dir is None:
dst_dir = ''
plt.clf()
plt.figure(figsize=(3, 3))
plt.rcParams["font.family"] = "Times New Roman"
for (x_val, y_val, a_val, string) in zip(recall, precision, area, name):
label = string + ' (AUC = %0.4f)' %a_val
plt.plot(x_val, y_val, label=label)
plt.legend(loc='lower right')
plt.xlim([0, 1])
plt.ylim([0, 1])
plt.grid()
plt.ylabel('Precision')
plt.xlabel('Recall')
plt.savefig(os.path.join(dst_dir, 'pr.pdf'), bbox_inches='tight')
plt.close()
def plot_det(fnr, fpr, name, dst_dir):
"""Plotting DET curve.
Arguments:
fnr {list} -- a list of numpy 1D array for true positive rate
fpr {list} -- a list of numpy 1D array for false positive rate
name {str} -- text for the legend
dst_dir {str} -- output figure directory, file name `det.pdf`
"""
plt.clf()
plt.figure(figsize=(3, 3))
plt.rcParams["font.family"] = "Times New Roman"
for (x_val, y_val, string) in zip(fpr, fnr, name):
plt.plot(x_val, y_val, label=string)
plt.legend(loc='upper right')
plt.xlim([0.01, 1])
plt.ylim([0.01, 1])
plt.xscale('log')
plt.yscale('log')
plt.ylabel('False Negative Rate')
plt.xlabel('False Positive Rate')
axes = plt.gca()
axes.xaxis.set_major_formatter(mpl.ticker.FormatStrFormatter('%.2f'))
axes.yaxis.set_major_formatter(mpl.ticker.FormatStrFormatter('%.2f'))
plt.savefig(os.path.join(dst_dir, 'det.pdf'), bbox_inches='tight')