-
Notifications
You must be signed in to change notification settings - Fork 0
/
plot_exp1_single_label.py
90 lines (60 loc) · 2.02 KB
/
plot_exp1_single_label.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
import numpy as np
import matplotlib.pyplot as plt
import os
import random
def plot_data(dataset, results):
#clear the file
f = open(r'./output/exp-1-graph/' + 'Accuracy' + '_' + dataset + '.data', 'w')
f.close()
f = open(r'./output/exp-1-graph/' + 'Accuracy' + '_' + dataset + '.data', 'a')
f.write(str(results))
f.close()
length = len(results[random.choice(results.keys())])
plt.figure()
plt.title(dataset)
plt.xlabel('Average Number of Answers for Each Task')
plt.ylabel('Accuracy')
plots = []
labels = []
for method in results:
labels.append('_'.join(method.split('_')[1:]))
X = results[method]
plots.append(plt.plot(range(1, length + 1 , 1), X, label='_'.join(method.split('_')[1:])))
plt.axis([0, length + 1 , 0 , 1])
plt.legend(loc ='lower right')
plt.savefig('./output/exp-1-graph/' + 'Accuracy' + '_' + dataset + '.png')
def get_datafile(datafile):
X = []
f = open(datafile,'r')
for line in f.xreadlines():
if not line:
continue
line = line.strip()
line = line.split('\t')
line_x = []
for item in line:
line_x.append(eval(item))
X.append(line_x)
f.close()
n_sample = len(X)
X = np.sum(X, axis=0) /n_sample
return X
def plot():
folder = r'./output/exp-1/single_label'
if not os.path.isdir(folder):
os.mkdir(folder)
datasets = os.listdir(folder)
for dataset in datasets:
if dataset[0] == '.':
continue
newfolder = folder + r'/' + dataset
methods = os.listdir(newfolder)
accuracy = {}
for method in methods:
if method[0] == '.':
continue
datafile = newfolder + r'/' + method
accuracy[method] = get_datafile(datafile)
plot_data(dataset, accuracy)
if __name__ == "__main__":
plot()