How to unscale data after predictions?

Viewed 2337

I have a dataset with 2 features (price & volume) & 1 predicted variable (price) and use LTSM model to predict next price based on the previous set of prices.

First I scale the dataset:

#Scale the data
scaler = MinMaxScaler(feature_range=(0,1))
scaled_data = scaler.fit_transform(dataset)

Finally I want to unscale it:

#Get the models predicted price values
predictions = model.predict(x_test)
predictions = scaler.inverse_transform(predictions)

But this doesn't work and I get this error:

ValueError: non-broadcastable output operand with shape (400,1) doesn't match the broadcast shape (400,2)
3 Answers

What this error means is that: you have scaled two features i.e. price and volume of shape (400,2), however, at the time of unscaling you are giving only the predicted price in shape (400,1)

A simple solution is to use two separate scalers - one that will unscale the response variable i.e. price (and the associated input feature, again the price), and second one for the rest of the features.

Did you try this:

predictions = scaler.inverse_transform(predictions.reshape(-1,1)).reshape(-1)

I decided to add zero-column to the predictions dataset, unscale it and then remove unused colunm. Stupid but works for me )

Related