7

I get the following error when I attempt to load a saved sklearn.preprocessing.MinMaxScaler

/shared/env/lib/python3.6/site-packages/sklearn/base.py:315: UserWarning: Trying to unpickle estimator MinMaxScaler from version 0.23.2 when using version 0.24.0. This might lead to breaking code or invalid results. Use at your own risk.
  UserWarning)
[2021-01-08 19:40:28,805 INFO train.py:1317 - main ] EXCEPTION WORKER 100: 
Traceback (most recent call last):
  ...
  File "/shared/core/simulate.py", line 129, in process_obs
    obs = scaler.transform(obs)
  File "/shared/env/lib/python3.6/site-packages/sklearn/preprocessing/_data.py", line 439, in transform
    if self.clip:
AttributeError: 'MinMaxScaler' object has no attribute 'clip'

I trained the scaler on one machine, saved it, and pushed it to a second machine where it was loaded and used to transform input.

# loading and transforming
import joblib
from sklearn.preprocessing import MinMaxScaler

scaler = joblib.load('scaler')
assert isinstance(scaler, MinMaxScaler)
data = scaler.transform(data)  # throws exception
Bobs Burgers
  • 761
  • 1
  • 5
  • 26

4 Answers4

11

The issue is you are training the scaler on a machine with an older verion of sklearn than the machine you're using to load the scaler.

Noitce the UserWarning

UserWarning: Trying to unpickle estimator MinMaxScaler from version 0.23.2 when using version 0.24.0. This might lead to breaking code or invalid results. Use at your own risk. UserWarning)

The solution is to fix the version mismatch. Either by upgrading one sklearn to 0.24.0 or downgrading to 0.23.2

Bobs Burgers
  • 761
  • 1
  • 5
  • 26
5

New property clip was added to MinMaxScaler in later version (since 0.24).

# loading and transforming
import joblib
from sklearn.preprocessing import MinMaxScaler

scaler = joblib.load('scaler')
assert isinstance(scaler, MinMaxScaler)
scaler.clip = False  # add this line
data = scaler.transform(data)  # throws exceptio

Explanation:

Becase clip is defined in __init__ method it is part of MinMaxScaler.__dict__. When you try to create object from pickle __setattr__ method is used to set all attributues, but clip was not used in older version therefore is missing in your new MinMaxScale instance. Simply add:

scaler.clip = False

and it should work fine.

Peter Trcka
  • 1,279
  • 1
  • 16
  • 21
4

I solved this issue with pip install scikit-learn==0.23.2 in my conda or cmd. Essentially downgrading the scikit module helped.

Sau
  • 99
  • 5
0

version issue of sklearn
You need to install in windows
pip install scikit-learn==0.24.0
I solve my Problem using this command

  • As it’s currently written, your answer is unclear. Please [edit] to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Oct 28 '21 at 06:50