1

Here is my model definition:

model = Sequential()
model.add(LSTM(i, input_shape=(None, 1), return_sequences=True))
model.add(Dropout(l))

model.add(LSTM(j))
model.add(Dropout(l))

model.add(Dense(k))
model.add(Dropout(l))

model.add(Dense(1))

and here is result

p = model.predict(x_test)
plt.plot(y_test)
plt.plot(p)

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

The figure shows almost a perfect match with gold test data and the predictions. Is it possible to predict with such high accuracy?

I think something is wrong because there's no volatility. So I wonder if it's been implemented properly.

If the implementation is correct, how can you get the following(next) value?

Is it right to do this implement?

a = x_test[-1]
b = model.predict(a)
c = model.predict(b)
...

To sum up the question:

  1. Is the implementation right way?

  2. I wonder how to get the value of the next data.

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

Mehdi
  • 4,202
  • 5
  • 20
  • 36
GoBackess
  • 404
  • 3
  • 17
  • could you provide an example of your data? if this is correct `model.predict(x_test)` then this is incorrect `model.predict(x_test[-1])`. – Mehdi Aug 03 '19 at 22:11
  • what do you mean by "the value of the next data"? – Mehdi Aug 03 '19 at 22:13
  • I have test data and using test dataset, I want to predict next day close value – GoBackess Aug 03 '19 at 22:14
  • https://docs.google.com/spreadsheets/d/1Q6Lh07Yg-OGtuz_Rvy979gcA65jrEqVNJNjndklCbg4/edit?usp=sharing here is my dataset and I use only close data – GoBackess Aug 03 '19 at 22:15
  • The model is not predicting a sequence. It only predicts one number. Oddly, your plot doesn't match any of the values. it seems you are plotting values between [0,1] but your sample data is not like this... – Mehdi Aug 03 '19 at 22:20
  • Because I preprocessed it. `scaler = MinMaxScaler(feature_range=(0, 1))` – GoBackess Aug 03 '19 at 22:22
  • the reason I ask for the data is to understand the question. It is definitely possible to get such accuracy if the degree of freedom is limited in feature space. – Mehdi Aug 03 '19 at 22:23
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/197430/discussion-between-mehdi-and-gobackess). – Mehdi Aug 03 '19 at 22:24

1 Answers1

1

Is the implementation right way?

Your code seems correct. I think you are not getting surprising results. You need to compare the results with a baseline that next prediction is randomly sampled from the range of possible day-to-day change. This way at least you can understand if your model is doing better than random sampling.

delta_train = train[1][1:] - train[1][:-1]
delta_range_train = delta_train.max()-delta_train.min()

# generating the baseline based on the change range in training:
random_p = test[0][:, -1] + (np.random.rand(test[0].shape[0])-0.5)*delta_range_train

You can check if your results are better than just a random sample random_p.

I wonder how to get the value of the next data.

this gives you the last data point in the test set:

a = x_test[-1:]

then, here you are predicting the next point day:

b = model.predict(a)

based on look_back you may need to keep some of the datapoints from to predict the next-next point:

c = model.predict(np.array([list(a[0,1:])+[b]])
Mehdi
  • 4,202
  • 5
  • 20
  • 36
  • I have a question so, if you can please help me https://stackoverflow.com/questions/57422128/how-can-you-get-the-followingnext-value-of-stock-pricetime-series – GoBackess Aug 09 '19 at 00:28