0
$\begingroup$

I have a dataset with three inputs X1,X2,X3. I tried to predict next value of X1 using lstm . I wrote the code for lstm and while trying model.predict I got an error cannot reshape array of size 4 into shape (4,3)

Here my code:

data= pd.DataFrame(data,columns=['X1','X2','X3','X4']) 
data.columns = ['X1', 'X2', 'X3','X4']
pd.options.display.float_format = '{:,.0f}'.format
data = data.dropna ()
y=data['Y'].astype(int)
cols=['X1', 'X2', 'X3','X4']
x=data[cols].astype(int)

data=data.values
scaler_x = preprocessing.MinMaxScaler(feature_range =(0, 1))
x = np.array(x).reshape ((len(x),4 ))
x = scaler_x.fit_transform(x)
scaler_y = preprocessing.MinMaxScaler(feature_range =(0, 1))
y = np.array(y).reshape ((len(y), 1))
y = scaler_y.fit_transform(y)

train_end = 80
x_train=x[0: train_end ,]
x_test=x[train_end +1: ,]
y_train=y[0: train_end]
y_test=y[train_end +1:] 
x_train=x_train.reshape(x_train.shape +(1,))
x_test=x_test.reshape(x_test.shape + (1,))    

num_time_steps=3
model = Sequential()
model.add(LSTM(4, return_sequences=True,input_shape=(num_time_steps,x_train_n.shape[2])))  # returns a sequence of vectors of dimension 32
model.add(LSTM(8, return_sequences=True))  # returns a sequence of vectors of dimension 32
model.add(LSTM(8))  # return a single vector of dimension 32
model.add(Dense(1))


batchsize = 1
model.compile(loss="mean_squared_error",optimizer="adam")
history = model.fit(x_train,y_train, batch_size = batchsize, nb_epoch=30,validation_data=(x_test, y_test),shuffle =True)

model.reset_states()
pred1=model.predict(x_test)

pred1 = []
for s in x_train:
   pred1.append(model.predict(s[0].reshape(x_train.shape[2], num_time_steps)))
   pred1 = np.array(pred1)

Error : Error

MY CSV FILE

Can anyone help me to solve this problem?

$\endgroup$
3
  • $\begingroup$ Show all code, especially when you create x_train. I supposed that you trying transform flat 4-element array into array with 3 dimension. $\endgroup$
    – fuwiak
    Commented Jul 19, 2019 at 11:43
  • $\begingroup$ @Paweł I upload my full code , now you may can look at it . Thank you $\endgroup$
    – bala
    Commented Jul 20, 2019 at 17:03
  • $\begingroup$ @Paweł no I have four elements and one output $\endgroup$
    – bala
    Commented Jul 21, 2019 at 16:21

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Browse other questions tagged or ask your own question.