-
Notifications
You must be signed in to change notification settings - Fork 33
/
Copy pathutils.py
61 lines (42 loc) · 2.06 KB
/
utils.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
# @Time : 2020/8/16
# @Author : LeronQ
# @github : https://github.com/LeronQ
# utils.py
# -*- coding: utf-8 -*-
import matplotlib.pyplot as plt
import numpy as np
import h5py
class Evaluation(object):
def __init__(self):
pass
@staticmethod
def mae_(target, output):
return np.mean(np.abs(target - output))
@staticmethod
def mape_(target, output):
return np.mean(np.abs(target - output) / (target + 5)) # 加5是因为target有可能为0,当然只要不太大,加几都行
@staticmethod
def rmse_(target, output):
return np.sqrt(np.mean(np.power(target - output, 2)))
@staticmethod
def total(target, output):
mae = Evaluation.mae_(target, output)
mape = Evaluation.mape_(target, output)
rmse = Evaluation.rmse_(target, output)
return mae, mape, rmse
def visualize_result(h5_file, nodes_id, time_se, visualize_file):
file_obj = h5py.File(h5_file, "r") # 获得文件对象,这个文件对象有两个keys:"predict"和"target"
prediction = file_obj["predict"][:][:, :, 0] # [N, T],切片,最后一维取第0列,所以变成二维了,要是[:, :, :1]那么维度不会缩减
target = file_obj["target"][:][:, :, 0] # [N, T],同上
file_obj.close()
plot_prediction = prediction[nodes_id][time_se[0]: time_se[1]] # [T1],将指定节点的,指定时间的数据拿出来
plot_target = target[nodes_id][time_se[0]: time_se[1]] # [T1],同上
plt.figure()
plt.grid(True, linestyle="-.", linewidth=0.5)
plt.plot(np.array([t for t in range(time_se[1] - time_se[0])]), plot_prediction, ls="-", marker=" ", color="r")
plt.plot(np.array([t for t in range(time_se[1] - time_se[0])]), plot_target, ls="-", marker=" ", color="b")
plt.legend(["prediction", "target"], loc="upper right")
plt.axis([0, time_se[1] - time_se[0],
np.min(np.array([np.min(plot_prediction), np.min(plot_target)])),
np.max(np.array([np.max(plot_prediction), np.max(plot_target)]))])
plt.savefig(visualize_file + ".png")