'Invert MinMaxScaler from scikit_learn

To feed my generative neural net, I need to normalize some data between -1 and 1.

I do it with MinMaxScaler from Sklearn and it works great. Now, my generator is going to output data between -1 and 1.

How to revert MinMaxScaler to get real data ?



Solution 1:[1]

Let us start by defining a pandas dataframe:

cols = ['A', 'B']
data = pd.DataFrame(np.array([[2,3],[1.02,1.2],[0.5,0.3]]),columns=cols)

enter image description here

The we scale the data using the MinMaxScaler

scaler = preprocessing.MinMaxScaler(feature_range = (0,1))
scaled_data = scaler.fit_transform(data[cols])

enter image description here

Now, to invert the transformation you should call the inverse transform:

scaler.inverse_transform(scaled_data)

enter image description here

Solution 2:[2]

def rev_min_max_func(scaled_val):
    max_val = max(df['target'])
    min_val = min(df['target'])
    og_val = (scaled_val*(max_val - min_val)) + min_val
    return og_val
df['pred_target'] = scaled_labeled_df['pred_scaled_target'].apply(lambda x: rev_min_max_func(x))

Even this works for me!

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 nunodsousa
Solution 2 Akshay Ijantkar