如何使用for循环获取清单的以下(下一个)股票价格(时间序列)值?

时间:2019-08-09 00:27:22

标签: python tensorflow keras lstm

这是我的代码

a = x_test[-1:]
b = model.predict(a)
c = model.predict(np.array([list(a[0,1:])+[b]]))

这是一天的预测代码

在此代码中

a = array([[[0.76165783],
        [0.7725424 ],
        [0.76774675],
        [0.7837351 ],
        [0.78315544],
        [0.7881376 ],
        [0.78365815],
        [0.79689795],
        [0.80051404],
        [0.8009032 ],
        [0.8078839 ],
        [0.80801773],
        [0.80524486],
        [0.8093028 ],
        [0.8162957 ],
        [0.82955176],
        [0.8293775 ],
        [0.83183414],
        [0.84109306],
        [0.84054583]]], dtype=float32)

b = array([[0.8390325]], dtype=float32)c = array([[0.8379273]], dtype=float32)

我试图预测更多的下一个值

predict = x_test[-1:]
b = model.predict(predict)
c = model.predict(np.array([list(predict[0,1:])+[b]]))

predict = np.array([list(predict[0,1:])+[b]])
d = model.predict(np.array([list(predict[0,1:])+[c]]))

predict = np.array([list(predict[0,1:])+[c]])
e = model.predict(np.array([list(predict[0,1:])+[d]]))

predict = np.array([list(predict[0,1:])+[d]])
f = model.predict(np.array([list(predict[0,1:])+[e]]))

对吗?我不确定

所以,我想知道如何使用for循环来获取d,e,f,g ....

顺序输入代表先前时间步长中的过去信号,输出正在预测下一时间步长中的信号。分割训练测试数据后,对测试数据的预测如下:

1

我想预测t + 1,t + 2 ... t + n。该模型预测t + 1,而另一个模型使用for循环预测t + n。

如何获得以下(下一个)值?

def create_dataset(signal_data, look_back=1):
    dataX, dataY = [], []
    for i in range(len(signal_data) - look_back):
        dataX.append(signal_data[i:(i + look_back), 0])
        dataY.append(signal_data[i + look_back, 0])
    return np.array(dataX), np.array(dataY)

train_size = int(len(signal_data) * 0.80)
test_size = len(signal_data) - train_size - int(len(signal_data) * 0.05)
val_size = len(signal_data) - train_size - test_size
train = signal_data[0:train_size]
val = signal_data[train_size:train_size+val_size]
test = signal_data[train_size+val_size:len(signal_data)]

x_train, y_train = create_dataset(train, look_back)
x_val, y_val = create_dataset(val, look_back)
x_test, y_test = create_dataset(test, look_back)

我将create_datasetlook_back=20一起使用。

signal_data经过最小-最大规格化MinMaxScaler(feature_range=(0, 1))预处理。

1 个答案:

答案 0 :(得分:1)

我会写一个像这样的函数:

def forecast_seq(model, init_seq, n_next_steps):
    results = []
    curr_seq = init_seq[:]
    for _ in range(n_next_steps):
        # predict the next step and update the current sequence
        pred_step = model.predict(np.array([curr_seq]))[0]
        curr_seq = np.concatenate([curr_seq[-1:], [pred_step]])
        results.append(pred_step)

    return results

您可以通过以下方式使用它:

# this will update the last datapoint with the predictions of the next 5 steps:
next_seq_in5 = forecast_seq(model, x_test[-1], 5)