-
Notifications
You must be signed in to change notification settings - Fork 8
/
plot_exp3_continuous.py
118 lines (79 loc) · 2.66 KB
/
plot_exp3_continuous.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
import numpy as np
import matplotlib.pyplot as plt
import os
import random
import sys
import math
def plot_data(dataset, results, metric):
#clear the file
f = open(r'./output/exp-3-graph/' + metric + '_' + dataset + '.data', 'w')
f.close()
f = open(r'./output/exp-3-graph/' + metric + '_' + dataset + '.data', 'a')
f.write(str(results))
f.close()
length = len(results[random.choice(results.keys())])
plt.figure()
plt.title(dataset)
plt.xlabel('Percentage of Known Truth (* 20% )')
plt.ylabel(metric)
plots = []
labels = []
mins = sys.maxint
maxs = -sys.maxint
for method in results:
tempmin = min(results[method])
tempmax = max(results[method])
mins = min(mins, tempmin)
maxs = max(maxs, tempmax)
mins = int(math.floor(mins)) - 1
maxs = int(math.ceil(maxs)) + 1
for method in results:
labels.append('_'.join(method.split('_')[1:]))
X = results[method]
plots.append(plt.plot(range(0, length , 1), X, label='_'.join(method.split('_')[1:])))
plt.axis([0, length + 1 , mins , maxs])
plt.legend(loc ='upper right')
plt.savefig('./output/exp-3-graph/' + metric + '_' + dataset + '.png')
def get_datafile(datafile):
X = []
f = open(datafile,'r')
for line in f.xreadlines():
line = line.strip()
if not line:
continue
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-3-graph'
if not os.path.isdir(folder):
os.mkdir(folder)
folder = r'./output/exp-3/continuous'
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)
MAE = {}
RMSE = {}
for method in methods:
if method[0] == '.':
continue
datafile = newfolder + r'/' + method
if method.startswith('MAE'):
MAE[method] = get_datafile(datafile)
if method.startswith('RMSE'):
RMSE[method] = get_datafile(datafile)
plot_data(dataset, MAE, 'MAE')
plot_data(dataset, RMSE, 'RMSE')
if __name__ == "__main__":
plot()