【问题标题】:Access attributes with sklearn Pipeline and MultiOutputRegressor使用 sklearn Pipeline 和 MultiOutputRegressor 访问属性
【发布时间】:2020-02-12 00:11:36
【问题描述】:

假设一个机器学习模型,比如 LightGBM 的LGBMRegressor,有一个属性best_iteration_。在调用fit 方法后如何访问此属性,从而利用了sklearn 的PipelineMultiOutputRegressor

对于Pipeline,我试过named_steps

foo.named_steps['reg']

返回以下对象sklearn.multioutput.MultiOutputRegressor.

然后,我尝试了.estimators_

foo.named_steps['reg'].estimators_

返回一个列表。但是,该列表包含提供给模型的初始参数。

有人能解释一下访问模型属性的理想方式吗?

【问题讨论】:

标签: python scikit-learn attributes pipeline


【解决方案1】:

我假设foo 是一个 sklearn 管道对象,如果是这样,您可能可以这样做:

for e in foo.named_steps['reg'].estimators_:
    print(e.best_iteration_)
  • foo.named_steps['reg'].estimators_ 返回估算器列表 MultiOutputRegressor 内部。
  • e 是您使用的 LGBMRegressor 在 MultiOutputRegressor 内部。

您可以将best_iteration_ 替换为您想要访问的模型的任何属性。

【讨论】:

  • 是的,foo 的类型为 sklearn.pipeline.Pipeline。但是,我尝试了您的for 循环建议,并返回了None
  • 根据documentation,我引用:The best iteration of fitted model if early_stopping_rounds has been specified.你需要指定early_stopping_rounds否则它会返回None。
猜你喜欢
  • 2020-04-16
  • 2018-01-24
  • 2021-10-03
  • 2012-12-07
  • 2022-01-12
  • 2021-06-17
  • 2019-06-05
  • 2014-09-07
  • 1970-01-01
相关资源
最近更新 更多