AttributeError:“MinMaxScaler”对象没有属性“clip”

Bob*_*ers 8 python scikit-learn joblib

当我尝试加载已保存的文件时出现以下错误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'
Run Code Online (Sandbox Code Playgroud)

我在一台机器上训练缩放器,保存它,然后将其推送到第二台机器,在第二台机器上加载它并用于转换输入。

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

scaler = joblib.load('scaler')
assert isinstance(scaler, MinMaxScaler)
data = scaler.transform(data)  # throws exception
Run Code Online (Sandbox Code Playgroud)

Bob*_*ers 12

问题是您正在使用比用于加载缩放器的机器更旧版本的 sklearn 的机器上训练缩放器。

注意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)

解决方案是修复版本不匹配的问题。通过将一个 sklearn 升级到0.24.0或降级到0.23.2


Pet*_*cka 5

clip在更高版本中添加了新属性MinMaxScaler(自 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
Run Code Online (Sandbox Code Playgroud)

解释:

因为caseclip是在__init__方法中定义的,所以它是MinMaxScaler.__dict__. 当您尝试从 pickle 创建对象时,__setattr__方法用于设置所有属性,但clip在旧版本中未使用,因此在新MinMaxScale实例中丢失。只需添加:

scaler.clip = False
Run Code Online (Sandbox Code Playgroud)

它应该工作正常。