I have input with 3 features and I want to predict only one feature. I want to split the data into 10 samples in each sequence and then train the LSTM model. My code is below.
def split_sequences(sequence_x, sequence_y, n_steps):
X, y = [], []
for i in range(0,len(sequence_x),n_steps):
X.append(sequence_x[i:i+n_steps])
y.append(sequence_y[i:i+n_steps])
X = np.array(X)
y = np.array(y)
return X,y
First I separated the feature that I would predictfrom the dataframe.
sequence_y = df['feature4'].to_list()
df = df.drop(columns = ['feature4'])
n_steps_s = 10
X, y = split_sequences(df.values.tolist(), sequence_y, n_steps_s)
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.33)
LSTM model:
n_features = len(X_train[0][0])
# define model
model = Sequential()
model.add(LSTM(100, activation='relu', return_sequences=True, input_shape=(n_steps_in, n_features)))
model.add(LSTM(100, activation='relu'))
model.add(Dense(n_steps_out))
model.compile(optimizer='adam', loss='mse')
# fit model
model.fit(X_train, y_train, epochs=2, verbose=1)
But then I get this error:
ValueError Traceback (most recent call last)
<ipython-input-682-fa5811eb8173> in <module>
8 model.compile(optimizer='adam', loss='mse')
9 # fit model
---> 10 model.fit(X_train, y_train, epochs=2, verbose=1)
.
.
.
.
ValueError: Failed to convert a NumPy array to a Tensor (Unsupported object type list).