Value Error while using scaler.inverse_transform in Python

2.3k views Asked by At

I am a beginner with Neural Networks and not very well aware with mathematics at the back end of scaling matrices using scaler.inverse_transform. I am using a tutorial to apply LSTM on my data and forecast time series for one of the variable. I am having this problem in prediction when I am scaling. The code is as below.

This is how I have trained Data.

from keras.models import Sequential
from keras.layers import Dense
from keras.layers import LSTM

# split into train and test sets
values = reframed.values
n_train_sec = 5000
train = values[:n_train_sec, :]
test = values[n_train_sec:, :]
# split into input and outputs
train_X, train_y = train[:, :-1], train[:, -1]
test_X, test_y = test[:, :-1], test[:, -1]
# reshape input to be 3D [samples, timesteps, features]
train_X = train_X.reshape((train_X.shape[0], 1, train_X.shape[1]))
test_X = test_X.reshape((test_X.shape[0], 1, test_X.shape[1]))
print(train_X.shape, train_y.shape, test_X.shape, test_y.shape)

This is how I designed model.

# design network
model = Sequential()
model.add(LSTM(50, input_shape=(train_X.shape[1], train_X.shape[2])))
model.add(Dense(1))
model.compile(loss='mae', optimizer='adam')
# fit network
history = model.fit(train_X, 
train_y,epochs=100,batch_size=160,validation_data=(test_X, test_y), 
verbose=2, shuffle=False)

and this is how I am trying to predict

from math import sqrt
from numpy import concatenate

# make a prediction
yhat = model.predict(test_X)
test_X = test_X.reshape((test_X.shape[0], test_X.shape[2]))
# invert scaling for forecast
inv_yhat = concatenate((yhat, test_X[:, 1:]), axis=1)
inv_yhat = scaler.inverse_transform(inv_yhat)
inv_yhat = inv_yhat[:,0]
# invert scaling for actual
test_y = test_y.reshape((len(test_y), 1))
inv_y = concatenate((test_y, test_X[:, 1:]), axis=1)
inv_y = scaler.inverse_transform(inv_y)
inv_y = inv_y[:,0]
# calculate RMSE
rmse = sqrt(mean_squared_error(inv_y, inv_yhat))
print('Test RMSE: %.3f' % rmse)

I am getting.

ValueError: operands could not be broadcast together with shapes (4599,12) 
(11,) (4599,12) 

Initially the shape of test_X was (4599, 1, 12). If somebody is interested to know more about data I can send data and html of iPython file.

0

There are 0 answers