【问题标题】:How to improve simple univariant time series forecasting?如何改进简单的单变量时间序列预测?
【发布时间】:2020-03-10 04:43:53
【问题描述】:

我有一个简单的单变量时间序列预测问题,我的输入是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 中是否有更有效的方法来做到这一点?

如果需要,我很乐意提供更多详细信息。

【问题讨论】:

    标签: python keras lstm


    【解决方案1】:

    在这样的问题中,可以通过考虑更多的先前时间步来获得更好的结果。

    另外,请注意 Jason 的 Brownlee 示例更适合我们了解如何为时间序列预测创建数据集。要做出正确的预测,需要更多的数据。

    毕竟,在单变量(未来预测只考虑一个特征)问题中,ARIMA、SARIMA 等更简单的(甚至是统计模型)问题可能更适合。

    【讨论】:

    • 更多数据是什么意思?是扩大窗口大小吗?谢谢你:)
    • 更多数据本质上意味着更大的数据集(例如,1.000.000 个元素的数组而不是 10 个元素的 raw_seq)。 window_size 是 n_steps_in,是的,可以通过改变这个 'n_steps_in' 参数来提高精度(在你的情况下增加它)。
    猜你喜欢
    • 2015-03-30
    • 2021-03-16
    • 1970-01-01
    • 2017-07-05
    • 2016-10-29
    • 2020-07-26
    • 2022-01-16
    • 2019-10-31
    • 2014-11-12
    相关资源
    最近更新 更多