GoBackess
GoBackess

Reputation: 414

how can I get the predict future 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)).

Here is my model definition:

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

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

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

model.add(Dense(1))

x_test shape is (1340, 20, 1) and

y_test shape is (1340,)

now... how can you get the following(next) value?

I want to get the next value like model.predict or etc...

I have x_test and x_test[-1] == t So, the meaning of the next value is t+1, t+2, .... t+n, in this example I want to get t+1, t+2 ... t+n

if you want to full source(including dataset) then you can see here https://gist.github.com/Lay4U/e1fc7d036356575f4d0799cdcebed90e

Upvotes: 1

Views: 116

Answers (1)

Achintha Ihalage
Achintha Ihalage

Reputation: 2440

say total_data is your total dataset and sc is your MinMaxScaler. Then, after training the network you could predict next n values by doing the following.

inputs = total_data[len(total_data) - n - look_back:].values

inputs = inputs.reshape(-1,1)
inputs = sc.transform(inputs)
X_test = []
for i in range(look_back, inputs.shape[0]):
    X_test.append(inputs[i-look_back:i, 0])
X_test = np.array(X_test)
X_test = np.reshape(X_test, (X_test.shape[0], X_test.shape[1], 1))
predicted_stock_price = model.predict(X_test)
predicted_stock_price = sc.inverse_transform(predicted_stock_price)

print predicted_stock_price.shape would yield a shape of (n, 1) which is the number of values you want to predict.

Upvotes: 1

Related Questions