4
$\begingroup$

If we use sklearn library's preprocessing.normalize() function to normalize our data before learning, like this:

preprocessing.normalize(training_set)
model.add(LSTM())

Should we do a denormalization to the result of LSTM to get predicted result in a true scale? If yes, how to denormalize?

$\endgroup$

2 Answers 2

3
$\begingroup$

The implication in your question is that you're normalising the target variable as well as the predictors. In general, I think that's probably not the right thing to be doing, and that you should be excluding the target from normalisation. That being the case, no inverse transformation after training should be necessary.

$\endgroup$
3
$\begingroup$

This can be done in Python using scaler.inverse_transform.

Consider a dataset that has been normalized with MinMaxScaler as follows:

# normalize dataset with MinMaxScaler
scaler = MinMaxScaler(feature_range=(0, 1))
dataset = scaler.fit_transform(dataset)

# Training and Test data partition
train_size = int(len(dataset) * 0.8)
test_size = len(dataset) - train_size
train, test = dataset[0:train_size,:], dataset[train_size:len(dataset),:]

# reshape into X=t-50 and Y=t
previous = 50
X_train, Y_train = create_dataset(train, previous)
X_test, Y_test = create_dataset(test, previous)

# reshape input to be [samples, time steps, features]
X_train = np.reshape(X_train, (X_train.shape[0], 1, X_train.shape[1]))
X_test = np.reshape(X_test, (X_test.shape[0], 1, X_test.shape[1]))

Upon generating the predictions from LSTM, the same can be converted back using scaler.inverse_transform as follows:

model = Sequential()
model.add(LSTM(4, input_shape=(1, previous)))
model.add(Dense(1))
model.compile(loss='mean_squared_error', optimizer='adam')
model.fit(X_train, Y_train, epochs=100, batch_size=1, verbose=2)
trainpred = model.predict(X_train)
testpred = model.predict(X_test)
trainpred = scaler.inverse_transform(trainpred)
Y_train = scaler.inverse_transform([Y_train])
testpred = scaler.inverse_transform(testpred)
Y_test = scaler.inverse_transform([Y_test])
predictions = testpred
$\endgroup$
1
  • $\begingroup$ May I ask, how would we inverse normalise the "out-of-time" forecast? For the completely new time-tick forecast $t+1$ we can't use the min/max, computed up to the $t$ tick - correct? $\endgroup$
    – Alex S.
    Commented Mar 11, 2021 at 21:48

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.