pythonmachine-learningscikit-learn

How to scale back predicted data with skLearn MinMaxScaler()?


I am creating a neural network to denoise music.

The input to the model is an array that is scaled from 0 to 1. This is achieved using sklearn MinMaxScaler. The original range of the data is from -1 to 1. The output of the model is also an array scaled from 0 to 1.

I cannot however scale the data back to -1 to 1 when predicting information.

My code is similar to:

data = load(data_path)
scaler = MinMaxScaler(feature_range = (0,1))
data = data.reshape(-1,1)
data = scaler.fit_transform(data)

model = load_model(model_path)
predicted_data = model.predict(data)

predicted_data = scaler.inverse_transform(predicted_data)

I however receive the error:

This MinMaxScaler instance is not fitted yet. Call 'fit' with appropriate arguments before using this method.

The data however is already fitted and I do not want to fit it again.

Why exactly am I getting this error? Shouldn't MinMaxScaler still be able to do inverse_transform on unfitted data?

Are there any suggestions around this error?


Solution

  • The error says it all, you need to call the fit and transform method separately, and not just fit_transform.

    data = load(data_path)
    data = data.reshape(-1, 1)
    scaler = MinMaxScaler(feature_range = (0,1)).fit(data)
    data = scaler.transform(data)
    
    model = load_model(model_path)
    predicted_data = model.predict(data)
    
    predicted_data = scaler.inverse_transform(predicted_data)