Dmitry Isakov
Dmitry Isakov

Reputation: 580

How to unscale data after predictions?

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)

Upvotes: 3

Views: 6405

Answers (4)

James Oluwadare
James Oluwadare

Reputation: 1

An option that may be worth trying is to manually unscale the model predictions by accessing the feature range that was saved in the scaler object. We can access the min and the max values using scaler.data_min_ and scaler.data_max_ respectively. The formula for the unscaled_value would vary based on the scaling method you have used. This one works for MinMaxScaler, so you would need to adjust the definition of the unscaled_value if you change the scaling method you use.

    # individually unscale the target variable
    def unscale(scaled_value):
        # if target variable is the first column, then, data_max_[0]
        unscaled_value = scaled_value * (scaler.data_max_[0] - scaler.data_min_[0]) + (scaler.data_min_[0])
        return unscaled_value

You can then use this function simply by writing

    unscale(predictions)

I hope this is helpful.

Upvotes: 0

Pierre
Pierre

Reputation: 23

Did you try this:

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

Upvotes: 0

Dmitry Isakov
Dmitry Isakov

Reputation: 580

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

Upvotes: 0

jdsurya
jdsurya

Reputation: 1376

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.

Upvotes: 1

Related Questions