MinMaxScaler for different shapes in fit and inverse_transform

Viewed 525

I am using MinMaxScaler from sklearn to scale the input to values between 0 and 1 and then process the data to obtain another vector. I use inverse_transform on the obtained vector to get back values in the original range. The shapes of the input to the fit_transform and the input to the inverse_transform are different. As an MWE I have provided the following code.

import numpy as np
from sklearn.preprocessing import MinMaxScaler

sc = MinMaxScaler(feature_range=(0, 1))
a = np.random.randint(0, 10, (10, 5))
b = sc.fit_transform(a) # b values are in [0, 1] range
c = np.random.rand(10, 30) # as an example, I have generated values between 0 and 1
d = sc.inverse_transform(c)

I run into the error

ValueError: operands could not be broadcast together with shapes (10,30) (5,) (10,30)

I understand it is because of the shape mismatch. But the shapes of inputs in my actual code are fixed and cannot be changed (and are also different from each other). How can I get this to work? Any help is appreciated.

2 Answers

What you're using learns a different transform for each of the 5 column vectors you give as the input. I guess you probably want to learn a fixed transform. I guess you can achieve it by vectorizing the matrix. I would suggest the below

mean = a.mean()
std = a.std()
# Transform
b = (a - mean) / std
# Inverse transform
c = np.random.rand(10, 30)
d = c * std + mean

This can be solved by writing your own inverse_transform function and using the attributes of the fitted scaler.

let's say I have scaled 5 features in an array of shape (n,5) and I have made a prediction for the feature at the second index. I can now use the function below to inverse the MinMaxScaling of only this feature:

def inverse_predictions(predictions,scaler,prediction_index=2):
    '''This function uses the fitted scaler to inverse predictions, 
    the index should be set to the position of the target variable'''
    
    max_val = scaler.data_max_[prediction_index]
    min_val = scaler.data_min_[prediction_index]
    original_values = (predictions*(max_val - min_val )) + min_val
    
    return original_values
    
Related