-
Notifications
You must be signed in to change notification settings - Fork 2
/
Evaluator.py
37 lines (30 loc) · 1.35 KB
/
Evaluator.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
"""
Created on 2018/10/21 by Chunhui Yin(yinchunhui.ahu@gmail.com).
Description:Evaluating experimental results.
"""
import numpy as np
from sklearn.metrics import mean_absolute_error, mean_squared_error
def evaluate(model, x_test, y_test, batch_size):
predictions = model.predict(x_test, batch_size=batch_size, verbose=0)
mae = mean_absolute_error(y_test, predictions)
rmse = np.sqrt(mean_squared_error(y_test, predictions))
return mae, rmse
# Save the evaluation results into file
def saveResult(modelName, resultPath, dataType, density, result, metrics):
if density:
fileID = open('%s/%s_%s_result_%.2f.txt' % (resultPath, modelName, dataType, density), 'w')
else:
fileID = open('%s/%s_%s_result.txt' % (resultPath, modelName, dataType), 'w')
fileID.write('Metric: ')
for metric in metrics:
fileID.write('| %s\t' % metric)
avgResult = np.average(result, axis=0)
fileID.write('\nAvg:\t')
np.savetxt(fileID, np.matrix(avgResult), fmt='%.4f', delimiter='\t')
minResult = np.min(result, axis=0)
fileID.write('Min:\t')
np.savetxt(fileID, np.matrix(minResult), fmt='%.4f', delimiter='\t')
fileID.write('\n==================================\n')
fileID.write('Detailed results for %d epochs:\n' % result.shape[0])
np.savetxt(fileID, result, fmt='%.4f', delimiter='\t')
fileID.close()