用移动窗口预测时间序列

时间:2019-05-16 00:55:22

标签: python time-series lstm prediction

我有一个看起来像这样的数据集:

var1(t-1) | var1(t) | var2(t-1) | var2(t) | var3(t-1) | var3(t)

和几行。

前5列是输入,最后一列是输出。我可以逐步进行预测,例如:

训练集:

1 | 2 | 3 | 4 | 5 ---> pred 1

1 | 2 | 3 | 4 | 5 ---> pred 2

1 | 2 | 3 | 4 | 5 ---> pred 3

测试集:

1 | 2 | 3 | 4 | 5 ---> pred 4

1 | 2 | 3 | 4 | 5 ---> pred 5

我要预测的是:

1 | 2 | 3 | 4 | 5 ---> pred 4

1 | 2 | 3 | 4 | pred 4 ---> pred 5

1 | 2 | 3 | 4 | pred 5 ---> pred 6

1 | 2 | 3 | 4 | pred 6 ---> pred 7

我的代码:

create train and test sets
values = scaled.values
split_point = int(len(values)*.80) 
train = values[:split_point, :]
test = values[split_point:, :]

分隔输入X和输出Y

train_X, train_Y = train[:, :-1], train[:, -1]
test_X, test_Y = test[:, :-1], test[:, -1]

将输入重塑为3D张量:

train_X = train_X.reshape((train_X.shape[0], train_X.shape[1], 1))
test_X = test_X.reshape((test_X.shape[0], test_X.shape[1], 1))

创建和训练模型

model = Sequential()
model.add(LSTM(10, input_shape=(train_X.shape[1], train_X.shape[2])))
model.add(Dense(1))
model.compile(loss='mae', optimizer='adam')
history = model.fit(train_X, train_Y, nb_epoch=50, batch_size=100, 
validation_split = 0.2, verbose=2, shuffle=False)

预测所有测试值

preds = model.predict(test_X)

根据先前的预测一次预测一个:

def moving_window(num_future_pred):
    preds_moving = []
    moving_test_window = [test_X[0,:].tolist()]
    #moving_test_window = np.array(moving_test_window)

    for i in range(len(test_Y)):
        pred_one_step = model.predict(moving_test_window)
        preds_moving.append(pred_one_step[0,0])
        pred_one_step = pred_one_step.reshape((1,1,1))
        moving_test_window = 

        np.concatenate((moving_test_window[:,:4,:], pred_one_step), axis= 1)
    return preds_moving

preds_moving = moving_window(len(test_Y))

它获得第一个预测权,因为它使用test_X[0,:]作为输入。但随后移动窗口,而不是将[1,2,3,4,pred4]作为输入[2,3,4,5, pred4],然后将[3,4,5, pred4, pred5]等作为输入。 如何修改moving_window函数以将test_X[i, :4]作为输入,例如2000中i中的range,并与单步预测相结合?

此外,这是整个测试集和一步预测的图:

Entire test_Y

One step predictions, with moving_window

0 个答案:

没有答案