1

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:

1

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

GoBackess
  • 404
  • 3
  • 17

1 Answers1

2

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)
Mehdi
  • 4,202
  • 5
  • 20
  • 36
  • when I execute `next_seqq = forecast_seq(model, x_test[-1], 20)`, then i get result as `array([[0.91156596], [0.9150167 ]], dtype=float32)` I expect 20 next value but get 2 value. I used recusion loop – GoBackess Aug 09 '19 at 13:03
  • and, when I print(curr_seq) 20 step, then I get like below https://imgur.com/nvXelzG, I think something is wrong because the value just increases like no have volatility – GoBackess Aug 09 '19 at 13:14
  • ow, so, you have only 2 days but you want to geberate 20 days in future? – Mehdi Aug 09 '19 at 14:21
  • I want ```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]])) ``` like this using 20 data predict next value and insert predicted value to 20 data and predict next value ... i think this method – GoBackess Aug 09 '19 at 14:24
  • is this working now? Sorry, I don't have the whole model setup. So, I have to guess about some parts. – Mehdi Aug 09 '19 at 14:55
  • 1
    https://drive.google.com/open?id=1bzwMV9qU-gR9fqqFpGWD7Xj1E7zboOsj It works, but there are many things to modify. So, I try this method https://stackoverflow.com/questions/48760472/how-to-use-the-keras-model-to-forecast-for-future-dates-or-events – GoBackess Aug 09 '19 at 15:04
  • what should I have to fix it? https://imgur.com/OfQX7B5 ValueError: Error when checking input: expected lstm_1_input to have 3 dimensions, but got array with shape (1, 2) – GoBackess Aug 09 '19 at 15:13
  • how can I to put the value of temp at the end of the currentStep[:,here of end,:]? – GoBackess Aug 09 '19 at 15:18