-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathev.py
More file actions
55 lines (43 loc) · 1.55 KB
/
ev.py
File metadata and controls
55 lines (43 loc) · 1.55 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
52
53
54
55
import sys
from sklearn import metrics
class Eval:
def __init__(self, pred, test):
y_true = []
for line in open(test):
label, _ = line.strip().split(" ", 1)
y_true.append(label)
self.y_true = y_true
self.categories = list(set(y_true))
self.y_pred = [line.strip() for line in open(pred)]
def print_confusion_matrix(self, y_true, y_pred):
print
print "{:^30}".format("Confusion matrix")
categories = sorted(self.categories)
labels = " ".join("{:>10}".format(c) for c in categories)
print "{:>10} {} {:>10}".format("gold\pred", labels, "total")
for cat, predictions in zip(categories, metrics.confusion_matrix(y_true, y_pred)):
vals = " ".join("{:>10d}".format(p) for p in predictions)
print "{:>10} {} {:>10}".format(cat, vals, sum(predictions))
print
def print_metrics(self,
print_confusion_matrix=False,
print_averages=False):
y_true = self.y_true
y_pred = self.y_pred
if print_confusion_matrix:
self.print_confusion_matrix(y_true, y_pred)
acc = metrics.accuracy_score(y_true, y_pred)
print "Accuracy: {:.4f}".format(acc)
idx = 0
d = {}
for l in self.categories:
d[l] = idx
idx += 1
print metrics.classification_report([d[y] for y in y_true],
[d[y] for y in y_pred],
target_names=self.categories)
if __name__ == '__main__':
pred = sys.argv[1]
test = sys.argv[2]
ev = Eval(pred, test)
ev.print_metrics()