I am trying to implement a simple RNN on a Y Variable and 2 X Variables. A dataframe is created with random numbers in it and is split to a train and test sets. The train set is used to train the RNN and the test set is used to predict and evaluate the model.
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import talib as tb
from sklearn.preprocessing import MinMaxScaler
from sklearn.model_selection import train_test_split
from keras.models import Sequential
from keras.layers import Dense
from keras.layers import LSTM
from keras.layers import Dropout
df = pd.DataFrame(np.random.randint(0,100,size=(5000, 3)), columns=['Var1', 'Var2', 'Var3'])
df_train = df.iloc[0:4800].values
df_test = df.iloc[4800:].values
sc = MinMaxScaler(feature_range=(0,1))
df_train_sc = sc.fit_transform(df_train)
df_test_sc = sc.fit_transform(df_test)
# Creating a data structure with 60 timesteps and 1 output
X_train = []
y_train = []
for i in range(60, df_train_sc.shape[0]):
X_train.append(df_train_sc[i-60:i, 0:3])
y_train.append(df_train_sc[i, 0:1])
X_train, y_train = np.array(X_train), np.array(y_train)
X_test = []
y_test = []
for i in range(60, df_test_sc.shape[0]):
X_test.append(df_test_sc[i-60:i, 0:3])
y_test.append(df_test_sc[i, 0:1])
X_test, y_test = np.array(X_test), np.array(y_test)
# Initialising the RNN
regressor = Sequential()
# Adding the first LSTM layer and some Dropout regularisation
regressor.add(LSTM(units = 50, return_sequences = True, input_shape = X_train.shape[1:]))
regressor.add(Dropout(0.2))
# Adding a second LSTM layer and some Dropout regularisation
regressor.add(LSTM(units = 50, return_sequences = True))
regressor.add(Dropout(0.2))
# Adding a third LSTM layer and some Dropout regularisation
regressor.add(LSTM(units = 50, return_sequences = True))
regressor.add(Dropout(0.2))
# Adding a fourth LSTM layer and some Dropout regularisation
regressor.add(LSTM(units = 50))
regressor.add(Dropout(0.2))
# Adding the output layer
regressor.add(Dense(units = 1))
# Compiling the RNN
regressor.compile(optimizer = 'adam', loss = 'mean_squared_error')
# Fitting the RNN to the Training set
regressor.fit(X_train, y_train, epochs = 100, batch_size = 32)
regressor.evaluate(X_test, y_test)
y_pred = regressor.predict(X_test)
y_pred = sc.inverse_transform(y_pred)
When I run the following code:
y_pred = sc.inverse_transform(y_pred)
I get the following error:
ValueError: non-broadcastable output operand with shape (140,1) doesn't match the broadcast shape (140,3)
What do I need to change in the code to fix the error?