ValueError:could not broadcast input array from shape (1221,) into shape (1221,7)

Viewed 34

I am working on my project titled 'Prediction of Stock price using Stacked LSTM', but I am getting this error "ValueError: could not broadcast input array from shape (1221,) into shape (1221,7)", in the line

trainPredictPlot[look_back:len(train_predict)+look_back, :] = train_predict

My code is:

import tensorflow as tf
from tensorflow.keras.models import load_model
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Dense, LSTM, Dropout
lstm_model = Sequential()
lstm_model.add(LSTM(50, return_sequences = True, input_shape = (100, 1)))
lstm_model.add(LSTM(50, return_sequences = True))
lstm_model.add(LSTM(50))
lstm_model.add(Dense(1))
lstm_model.compile(loss = 'mean_squared_error', optimizer = 'adam')

lstm_model.summary()

lstm_model.fit(X_train, Y_train, validation_data = (X_test, Y_test), epochs = 100, batch_size = 64, verbose = 1)


train_predict = lstm_model.predict(X_train)
test_predict = lstm_model.predict(X_test)
train_predict.shape
test_predict.shape

train_predict_dataset_like = np.zeros(shape=(len(train_predict), 7) )
# put the predicted values in the right field
train_predict_dataset_like[:,0] = train_predict[:,0]
# inverse transform and then select the right field
train_predict = scaler.inverse_transform(train_predict_dataset_like)[:,0]

test_predict_dataset_like = np.zeros(shape=(len(test_predict), 7) )
# put the predicted values in the right field
test_predict_dataset_like[:,0] = test_predict[:,0]
# inverse transform and then select the right field
test_predict = scaler.inverse_transform(test_predict_dataset_like)[:,0]
import math

from sklearn.metrics import mean_squared_error
math.sqrt(mean_squared_error(Y_train, train_predict))

math.sqrt(mean_squared_error(Y_test, test_predict))

### Plotting 
# shift train predictions for plotting
look_back=100
trainPredictPlot = numpy.empty_like(data)
trainPredictPlot[:, :] = numpy.nan

##### This is where the error takes place ####
trainPredictPlot[look_back:len(train_predict)+look_back, :] = train_predict

# shift test predictions for plotting
testPredictPlot = numpy.empty_like(data)
testPredictPlot[:, :] = numpy.nan

##### This is where the error takes place ####
testPredictPlot[len(train_predict)+(look_back*2)+1:len(data)-1, :] = test_predict


# plot baseline and predictions
plt.plot(scaler.inverse_transform(data))
plt.plot(trainPredictPlot)
plt.plot(testPredictPlot)
plt.show()

The shape of train_predict and test_predict are (1221, 1) & (1221, 1) respectively.

1 Answers

I guess the shape of trainPredictPlot should be (num, 7), and trainPredictPlot[look_back:len(train_predict)+look_back, :] means to take all the columns of trainPredictPlot, so in the end you will get (1221, 7), which doesn't match train_predict. So you need to do this:

trainPredictPlot[look_back:len(train_predict)+look_back, 1] = train_predict

The 1 above means to take the first column, you need to change it according to the actual situation.

Related