user2559578
user2559578

Reputation: 153

How to reshape input for keras LSTM?

I have a numpy array of some 5000 rows and 4 columns (temp, pressure, speed, cost). So this is of the shape (5000, 4). Each row is an observation at a regular interval this is the first time i'm doing time series prediction and I'm stuck on input shape. I'm trying to predict a value 1 timestep from the last data point. How do I reshape it into the 3D form for LSTM model in keras?

Also It will be much more helpful if a small sample program is written. There doesn't seem to be any example/tutorial where the input has more than one feature (and also not NLP).

Upvotes: 0

Views: 1473

Answers (1)

mpariente
mpariente

Reputation: 660

The first question you should ask yourself is :

  • What is the timescale in which the input features encode relevant information for the value you want to predict?

Let's call this timescale prediction_context.

You can now create your dataset :

import numpy as np

recording_length = 5000
n_features = 4
prediction_context = 10  # Change here
# The data you already have
X_data = np.random.random((recording_length, n_features))
to_predict = np.random.random((5000,1))
# Make lists of training examples
X_in = []
Y_out = []
# Append examples to the lists (input and expected output)
for i in range(recording_length - prediction_context):
    X_in.append(X_data[i:i+prediction_context,:])
    Y_out.append(to_predict[i+prediction_context])

# Convert them to numpy array
X_train = np.array(X_in)
Y_train = np.array(Y_out)

At the end :
X_train.shape = (recording_length - prediction_context, prediction_context, n_features)
So you will need to make a trade-off between the length of your prediction context and the number of examples you will have to train your network.

Upvotes: 2

Related Questions