【问题标题】:Change a parameter of a sklearn pipeline更改 sklearn 管道的参数
【发布时间】:2021-01-05 20:59:29
【问题描述】:

我有一个Pipeline,我使用pickle 训练并保存了它。它包含以下步骤:

Pipeline(steps=[('preprocessing': Preprocessor()),
('my_transformer': my_transformer()), 
('model': XGBClassifier())
])

我想在执行my_transformer() 时记录一些信息,但仅在我预测概率时,即当我运行pipeline.predict_proba() 时。我不希望在运行 pipeline.predict() 时执行日志记录行。

my_transformer() 是这样的:

class my_transformer(BaseEstimator, TransformerMixin):
  def __init__(self, flag_log=False):
    self.flag_log = flag_log

  def transform(self, features):
    #apply transformations
    if self.flag_log:
      logger.info("log probabilities")

我要做的是根据我是否要记录信息来修改flag_log 的值。基本上,我想要这样的东西:

pipeline.set_params(my_transformer__flag_log=True)
probabilities = pipeline.predict_proba(features)
pipeline.set_params(my_transformer__flag_log=False)
predictions = pipeline.predict(features)

我尝试了上面的代码,但它不起作用,flag_log 的值没有改变。 还有其他解决方案吗?

【问题讨论】:

    标签: python scikit-learn pipeline


    【解决方案1】:

    要修改 flag_log 的值,只需运行:

    pipeline.set_params(my_transformer__flag_log=False)
    

    这应该对我有用。否则,如果您确定它不能以这种方式工作,请提供您的代码minimal, reproducible example,以便其他人可以重现您的问题(我会调查并更新我的答案)。

    【讨论】:

      猜你喜欢
      • 2020-09-10
      • 2017-06-13
      • 2021-09-01
      • 2018-06-24
      • 2017-11-26
      • 2021-09-25
      • 1970-01-01
      • 2020-04-15
      • 2017-03-12
      相关资源
      最近更新 更多