-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsvr.py
More file actions
132 lines (107 loc) · 3.8 KB
/
svr.py
File metadata and controls
132 lines (107 loc) · 3.8 KB
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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
import numpy as np
from sklearn.svm import SVR
from sklearn.model_selection import train_test_split
from sklearn.preprocessing import StandardScaler
import matplotlib.pyplot as plt
def mask_np(array, null_val):
if np.isnan(null_val):
return (~np.isnan(null_val)).astype('float32')
else:
return np.not_equal(array, null_val).astype('float32')
def masked_mape_np(y_true, y_pred, null_val=np.nan):
with np.errstate(divide='ignore', invalid='ignore'):
mask = mask_np(y_true, null_val)
mask /= mask.mean()
mape = np.abs((y_pred - y_true) / y_true)
mape = np.nan_to_num(mask * mape)
return np.mean(mape) * 100
def masked_rmse_np(y_true, y_pred, null_val=np.nan):
mask = mask_np(y_true, null_val)
mask /= mask.mean()
mse = (y_true - y_pred) ** 2
return np.sqrt(np.mean(np.nan_to_num(mask * mse)))
def masked_mae_np(y_true, y_pred, null_val=np.nan):
mask = mask_np(y_true, null_val)
mask /= mask.mean()
mae = np.abs(y_true - y_pred)
return np.mean(np.nan_to_num(mask * mae))
# def mape(y_true, y_pred):
# temp = np.abs((y_pred - y_true) / y_true)
# return np.mean(temp)
#
#
# def rmse(y_true, y_pred):
# temp = (y_true - y_pred) ** 2
# return np.sqrt(np.mean(temp))
#
#
# def mae(y_true, y_pred):
# temp = np.abs(y_true - y_pred)
# return np.mean(temp)
# 标准化
# ss_x = StandardScaler()
# x = ss_x.fit_transform(x)
# ss_y = StandardScaler()
# y = ss_y.fit_transform(y.reshape(-1, 1)).squeeze()
# 切割数据集
def get_data(data, step, point):
x = np.zeros((step, data.shape[0] - step))
y = np.zeros((step, data.shape[0] - step))
for i in range(data.shape[0] - step * 2):
x[:, i] = data[i: i + step, point]
y[:, i] = data[i + step: i + step * 2, point]
x = x.transpose()
y = y.transpose()
# print(x.shape)
x_train, x_test, y_train, y_test = train_test_split(x, y, test_size=0.2)
return x_train, x_test, y_train, y_test
# svr,预测12个时间片
def predict(x_train, x_test, y_train, pred_time):
svr = SVR(kernel='linear', C=0.1)
svr.fit(x_train, y_train[:, 0])
y = svr.predict(x_test).reshape(-1, 1)
for i in range(11):
x_test = np.delete(x_test, 0, axis=1)
x_test = np.append(x_test, y[:, -1].reshape(-1, 1), axis=1)
y = np.append(y, svr.predict(x_test).reshape(-1, 1), axis=1)
return y
data = np.load('data/PEMS03/PEMS03.npz')['data']
data = data.squeeze()
# data = np.load('data/PEMS07/PEMS07.npz')['data']
# data = data.squeeze()
# data = data[:17568]
print(data.shape)
step = 12
pred_time = 12
y_true3 = []
y_true2 = []
y_true1 = []
y_pred3 = []
y_pred2 = []
y_pred1 = []
# 每个观察点分别预测
for point in range(data.shape[1] - 2):
x_train, x_test, y_train, y_test = get_data(data, step, point)
y_true3.append(y_test)
y_pred3.append(predict(x_train, x_test, y_train, pred_time))
y_true2.append(y_true3[-1][:, :6])
y_pred2.append(y_pred3[-1][:, :6])
y_true1.append(y_true3[-1][:, :3])
y_pred1.append(y_pred3[-1][:, :3])
if point % 10 == 0:
print(point)
y_pred3 = np.array(y_pred3).reshape(-1)
y_true3 = np.array(y_true3).reshape(-1)
y_pred2 = np.array(y_pred2).reshape(-1)
y_true2 = np.array(y_true2).reshape(-1)
y_pred1 = np.array(y_pred1).reshape(-1)
y_true1 = np.array(y_true1).reshape(-1)
print('1 hour')
print('mae \t\t rmse \t\t mape')
print(masked_mae_np(y_true3, y_pred3, 0), masked_rmse_np(y_true3, y_pred3, 0), masked_mape_np(y_true3, y_pred3, 0))
print('30 minutes')
print('mae \t\t rmse \t\t mape')
print(masked_mae_np(y_true2, y_pred2, 0), masked_rmse_np(y_true2, y_pred2, 0), masked_mape_np(y_true2, y_pred2, 0))
print('15 minutes')
print('mae \t\t rmse \t\t mape')
print(masked_mae_np(y_true1, y_pred1, 0), masked_rmse_np(y_true1, y_pred1, 0), masked_mape_np(y_true1, y_pred1, 0))