-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathresult_to_conf_matrix.py
75 lines (51 loc) · 2 KB
/
result_to_conf_matrix.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
import matplotlib.pyplot as plt
import numpy as np
from docopt import docopt
def load_data_to_matrix(file_path, data_type):
with open(file_path) as f_in:
content = [line.strip() for line in f_in.readlines() if line.startswith(data_type)]
data = []
for line in content:
splitted = line.split(' ')
amount = int(splitted[1])
splitted_labels = splitted[0].split('-')
gold = splitted_labels[1]
predicted = splitted_labels[2]
data.append((gold, predicted, amount))
def count_items(lbl_gold, lbl_predicted):
for g, p, a in data:
if g == lbl_gold and p == lbl_predicted:
return a
return 0
# to matrix
labels_gold = [str(v) for v in sorted([int(v) for v in set([gold for gold, _, __ in data])])]
labels_predicted = [str(v) for v in sorted([int(v) for v in set([predicted for gold, _, __ in data])])]
matrix = np.zeros((len(labels_gold), len(labels_predicted)))
for idx_gold in range(len(labels_gold)):
for idx_predicted in range(len(labels_predicted)):
matrix[idx_gold, idx_predicted] = count_items(labels_gold[idx_gold], labels_predicted[idx_predicted])
return (labels_gold, labels_predicted, matrix)
def plot_confusion_matrix(matrix, labels_x, labels_y, title):
fig, ax = plt.subplots()
cax = ax.imshow(matrix, origin='upper')
width, height = matrix.shape
fig.colorbar(cax)
plt.xticks(np.arange(len(labels_x)), labels_x, rotation=45)
plt.yticks(np.arange(len(labels_y)), labels_y)
plt.xlabel('Predicted')
plt.ylabel('Gold')
for x in range(width):
for y in range(height):
plt.annotate(str(matrix[x,y]), size=6, xy=(y, x), horizontalalignment='center', verticalalignment='center')
plt.show()
def main():
args = docopt("""Plot the confusion matrix of a file.
Usage:
result_to_conf_matrix.py plot <path> <data>
""")
file_path = args['<path>']
data_type = args['<data>']
gold, predicted, matrix = load_data_to_matrix(file_path, data_type)
plot_confusion_matrix(matrix, predicted, gold, 'Confusion matrix')
if __name__ == '__main__':
main()