如何通过/跳过管道取决于某些条件 [英] How to passthrough / skip pipeline depend on certain condition

查看:44
本文介绍了如何通过/跳过管道取决于某些条件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下 scikit 学习管道用于一些数据预处理.

I have the following scikit learn pipeline used for some data preprocessing.

如果数据框中有分类特征,我想提取特征并通过SimpleImputer运行;如果没有这样的功能(即 dataframe['categoricals'] 不存在),我希望它简单地跳过"/通过管道并继续下一步.

If there is categorical feature in a dataframe, I would like to extract the features and run through SimpleImputer; if there is no such feature (i.e., dataframe['categoricals'] does not exist), I would like it simply "skip"/passthrough the pipeline and proceed to the next step.

如何实现?

Pipeline ([
('extract', extract_feature(dataframe['categoricals]),
('fill', SimpleImputer(strategy='constant', fill_value='dummy')

])

推荐答案

  1. 在转换器周围实现一个包装器以给它一个参数,例如,if_skip,并打开/关闭此参数以启用/禁用此转换器.当然,您可以将 if_skip 设置为实例变量,例如 self.if_skip,并根据需要分配上一个管道步骤的值

  1. implement a wrapper around the transformer to give it an argument, e.g., if_skip, and toggle on/off this argument to enable/disable this transformer. Of course, you can set if_skip as a instance variable, e.g., self.if_skip, and assign the value from a previous pipeline step if you wish

SkipSimpleImputer(if_skip=False,strategy='constant', fill_value='dummy')

class SkipSimpleImputer(SimpleImputer):
def __init__(if_skip=False, strategy='constant', fill_value='dummy')
    pass

  • 用if else :(,不是很好的解决方案,但至少它是一个解决方案,

  • wrap it with if else :(, not nice solution, but at least it is a solution,

    这篇关于如何通过/跳过管道取决于某些条件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

  • 查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆