【发布时间】: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