Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

AttributeError: 'MinMaxScaler' object has no attribute 'clip'

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
like image 833
Bobs Burgers Avatar asked Dec 17 '22 12:12

Bobs Burgers


2 Answers

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

like image 89
Bobs Burgers Avatar answered Dec 30 '22 14:12

Bobs Burgers


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.

like image 42
Peter Trcka Avatar answered Dec 30 '22 14:12

Peter Trcka