how can you get the following(next) value of stock price(time series) with list using for loop?

穿精又带淫゛_ 提交于 2019-12-13 03:41:32

问题


here is my code

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

this is one day predict code

in this code

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)

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

I tried to predict more next value

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]]))

is this right? I'm not sure

So, I want to know how to get d, e, f, g .... with list using for loop

The sequential input represents the past signal in previous time-steps, the output is predicting the signal in next time-step. After splitting the training and testing data, the predictions on the test data is as follows:

and I want to predicting t+1, t+2 ... t+n. model predicts t+1 while another predicts t+n using for loop.

how can you get the following(next) value?

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)

I use create_dataset with look_back=20.

signal_data is preprocessed with min-max normalisation MinMaxScaler(feature_range=(0, 1)).


回答1:


I would write a function like this:

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

You can use it this way:

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


来源:https://stackoverflow.com/questions/57422128/how-can-you-get-the-followingnext-value-of-stock-pricetime-series-with-list

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!