python - 如何改进简单的单变量时间序列预测?
问题描述
我有一个简单的单变量时间序列预测问题,我的输入是10, 20, 30, 40, 50, 60, 70, 80, 90
. 我希望模型预测接下来的 3 个值。即输出应该是100, 110, 120
。
我使用堆叠 LSTM 来做到这一点。我的代码如下。
from numpy import array
from keras.models import Sequential
from keras.layers import LSTM, Dense
def split_sequence(sequence, n_steps_in, n_steps_out):
X, y = list(), list()
for i in range(len(sequence)):
# find the end of this pattern
end_ix = i + n_steps_in
out_end_ix = end_ix + n_steps_out
# check if we are beyond the sequence
if out_end_ix > len(sequence):
break
# gather input and output parts of the pattern
seq_x, seq_y = sequence[i:end_ix], sequence[end_ix:out_end_ix]
X.append(seq_x)
y.append(seq_y)
return array(X), array(y)
raw_seq = [10, 20, 30, 40, 50, 60, 70, 80, 90]
# choose a number of time steps
n_steps_in, n_steps_out = 5, 3
# split into samples
X, y = split_sequence(raw_seq, n_steps_in, n_steps_out)
# reshape from [samples, timesteps] into [samples, timesteps, features]
n_features = 1
X = X.reshape((X.shape[0], X.shape[1], n_features))
model = Sequential()
model.add(LSTM(100, activation='relu', return_sequences=True, input_shape=(n_steps_in, n_features)))
model.add(LSTM(100, activation='relu'))
model.add(Dense(n_steps_out))
model.compile(optimizer='adam', loss='mse')
model.fit(X, y, epochs=50, verbose=0)
# demonstrate prediction
x_input = array([50, 60, 70, 80, 90])
x_input = x_input.reshape((1, n_steps_in, n_features))
yhat = model.predict(x_input, verbose=0)
print(yhat)
由于我使用的是一个非常简单的示例,因此我希望模型能够准确预测。但是,我得到的输出很糟糕([[135.52011 151.59491 175.79674]]
)。
我想知道在 LSTM 中是否有更有效的方法来做到这一点?
如果需要,我很乐意提供更多详细信息。
解决方案
在这样的问题中,通过考虑更多的先前时间步长可以获得更好的结果。
另外,请考虑到 Jason 的 Brownlee 示例更适合我们了解如何为时间序列预测创建数据集。对于体面的预测,需要更多的数据。
毕竟,在单变量(未来预测只考虑一个特征)问题中,更简单的(甚至是统计模型)如 ARIMA、SARIMA 可能更适合。
推荐阅读
- python - 将数据框列从字符串转换为日期为过去 2262 年的列
- kubernetes - 安装后正在重新创建 Istio ingress pod
- c - 难以理解指向指针增量值的指针
- python - 为什么 python pickle 对 unix 不起作用?
- python - 如何在 matplotlib 中编辑图例的文本颜色?
- vue.js - AWS Cloudfront 托管网站中的 Vue.js 路由
- java - Mac 上的 Java Xattjr - 无法加载资源:libxattrj.dylib
- vue.js - 订购 vue.js 文件的最佳实践
- c# - 如何正确等待来自无模式窗口的输入
- php - 在 PHP 中将布尔表达式解析为 MySql 查询