来自 scikit-learn 的 FutureWarning get_params

FutureWarning get_params from scikit-learn

我收到警告

File "[...]\lib\threading.py", line 890, in _bootstrap
  self._bootstrap_inner()
File "[...]\lib\threading.py", line 932, in _bootstrap_inner
  self.run()
File "C:\Program Files\JetBrains\PyCharm 2019.1.3\plugins\python\helpers\pydev\_pydevd_bundle\pydevd_comm.py", line 216, in run
  self._on_run()
File "C:\Program Files\JetBrains\PyCharm 2019.1.3\plugins\python\helpers\pydev\_pydev_bundle\pydev_console_commands.py", line 30, in _on_run
  values.append(pydevd_thrift.var_to_struct(var_obj, name, evaluate_full_value=True))
File "C:\Program Files\JetBrains\PyCharm 2019.1.3\plugins\python\helpers\pydev\_pydevd_bundle\pydevd_thrift.py", line 305, in var_to_struct
  value = format % v
File "[...]\lib\site-packages\sklearn\base.py", line 263, in __repr__
  repr_ = pp.pformat(self)
File "[...]\lib\pprint.py", line 153, in pformat
  self._format(object, sio, 0, 0, {}, 0)
File "[...]\lib\pprint.py", line 170, in _format
  rep = self._repr(object, context, level)
File "[...]\lib\pprint.py", line 404, in _repr
  repr, readable, recursive = self.format(object, context.copy(),
File "[...]\lib\site-packages\sklearn\utils\_pprint.py", line 169, in format
  return _safe_repr(object, context, maxlevels, level,
File "[...]\lib\site-packages\sklearn\utils\_pprint.py", line 415, in _safe_repr
  params = object.get_params(deep=False)
File "[...]\lib\site-packages\sklearn\base.py", line 193, in get_params
  warnings.warn('From version 0.24, get_params will raise an '
File "[...]\lib\warnings.py", line 109, in _showwarnmsg
  sw(msg.message, msg.category, msg.filename, msg.lineno,
File "<input>", line 59, in warn_with_traceback
[...]\lib\site-packages\sklearn\base.py:193: FutureWarning: From version 0.24, get_params will raise an AttributeError if a parameter cannot be retrieved as an instance attribute. Previously it would return None.
warnings.warn('From version 0.24, get_params will raise an '

尝试使用 sklearn 时。我不知道如何找出解决方法。显然这对你来说信息不多,但我什至不知道从哪里开始追查这个问题。

我唯一可以补充的是,当我尝试实例化我写的继承自 CalibratedClassifierCV.

的 class 时会发生这种情况

我该如何解决这个问题?

我想,我能找到答案是靠运气。正如 scikit-learn 的 documentation 中所述,初始化 必须 看起来像

def __init__(self, param1=1, param2=2):
    self.param1 = param1
    self.param2 = param2

我犯的错误是写了类似

的东西
def __init__(self, param1=1, param2=2):
    self.param1 = param1
    self.param2_ = param2

注意下划线。必须避免这种情况,因为它会混淆方法 set_paramsget_params。确实,我的警告源于后者。

另外尾部下划线是 estimated attributes:

Attributes that have been estimated from the data must always have a name ending with trailing underscore, for example the coefficients of some regression estimator would be stored in a coef_ attribute after fit has been called.

The estimated attributes are expected to be overridden when you call fit a second time.