使用神经网络Python进行多步时间序列预测会得出错误的结果

时间:2019-04-04 10:25:24

标签: python neural-network time-series forecasting

我有长达1年的每小时数据。我想根据以前的小时数据进行下一个小时的预测。我使用多层感知器来执行此操作,但是它不断给出错误的预测结果

为简单起见,假设我要使用前3小时的数据来预测接下来的1小时。这是我的数据示例(以T-2,T-1,T作为输入,T + 1作为目标):

T-2  T-1  T0  T+1
10   20   30   40
20   30   40   50
30   40   50   60
40   50   60   70

如果我向模型输入(20,30,40),它给我40而不是50,那么对于另一组数据也是如此(30,40,50产生50而不是60并很快产生)。我尝试了不同的时间范围,不同数量的隐藏层,神经元,历元,批处理大小以及不同的激活函数,但结果始终相同。产生的结果总是比(T + 1)更接近T0

n_input = Xtrain.shape[1] * Xtrain.shape[2] 
Xtrain = Xtrain.reshape((Xtrain.shape[0], n_input))
Xtest = Xtest.reshape((Xtest.shape[0], n_input))

# define model
model72 = Sequential()
model72.add(Dense(20, activation='relu', input_dim=n_input))
model72.add(Dense(1))
# Compile model
model72.compile(loss = 'mean_squared_error', metrics = ['mse', 'acc'], optimizer='adam')

# fit model
history72 = model72.fit(Xtrain, ytrain, validation_split=0.33, epochs=200, batch_size=1000, verbose=0)

我错过了什么吗?

这是我的数据,实际上是这样的:

[0.75400747 7.2062848  2.0027392 ] 4.194725333
[0.65565867 8.0780128  1.9312128 ] 4.4242058669999995
[2.01317013 7.2152256  3.22911893] 3.029441067
[1.989328   7.554976   4.43314667] 3.106928
[2.40656533 6.98574507 5.20652587] 3.2097472000000002
[2.65690773 7.41490347 6.05143147] 2.023601067
[1.5825216 7.6935584 6.9648832] 1.56464
[2.2754336  8.404352   6.56552747] 2.1621834669999997
[5.44792747 9.24180693 8.28961173] 3.8981888
[7.70547947 9.71566933 9.72908053] 5.889006932999999
[9.61285013 9.01977707 9.57857707] 5.702740267
[10.7423712  10.2550976   9.25968853] 7.347847467
[10.82581867  9.29545173  9.28651093] 7.9811541329999995
[10.89287467  9.2492576   8.9720928 ] 7.194363732999999
[10.6127296   9.34462613  9.46085653] 8.0154272
[11.17749013  9.05852053  9.4191328 ] 7.262909867
[10.74088107 10.72896     9.19859307] 7.2644
[10.08373227 10.9435392   8.9542112 ] 6.7070901329999995

1 个答案:

答案 0 :(得分:0)

首先,我不了解您如何定义输入,

n_input = Xtrain.shape[1] * Xtrain.shape[2] 
Xtrain = Xtrain.reshape((Xtrain.shape[0], n_input))
Xtest = Xtest.reshape((Xtest.shape[0], n_input))

据我所见,您的一个样本是3个值。

如果您有一个m by n矩阵,说m行(样本)和n个特征(在这种情况下,我认为n=3) you can input the matrix as such in the network as input. It will select rows as samples. Do not reshape it. X_train must have dimensions [samples,features]`。

在其他情况下,图像可能具有更大的尺寸,但在这种情况下,您应该具有具有此尺寸的表格数据。

因此在您的input_dim中,您必须表示:input_dim = 3

希望这会有所帮助