• 首页 首页 icon
  • 工具库 工具库 icon
    • IP查询 IP查询 icon
  • 内容库 内容库 icon
    • 快讯库 快讯库 icon
    • 精品库 精品库 icon
    • 问答库 问答库 icon
  • 更多 更多 icon
    • 服务条款 服务条款 icon

Sklearn自定义转换器:使用FunctionTransformer和子类化TransformerMixin:间的区别

用户头像
it1352
帮助1

问题说明

为了执行适当的CV,建议使用流水线,以便可以对CV中的每个折叠应用相同的转换.我可以使用 sklearn.preprocessing.FunctionTrasformer 子类化sklearn.base.TransformerMixin 来定义自定义转换.推荐哪种方法?为什么?

In order to do proper CV it is advisable to use pipelines so that same transformations can be applied to each fold in the CV. I can define custom transformations by using either sklearn.preprocessing.FunctionTrasformer or by subclassing sklearn.base.TransformerMixin. Which one is the recommended approach? Why?

正确答案

#1

完全由您决定,两者都会或多或少地取得相同的结果,只是编写代码的方式有所不同.

Well it is totally upto you, both will achieve the same results more or less, only the way you write the code differs.

例如,在使用 sklearn.preprocessing.FunctionTransformer 时,您可以简单地定义要使用的函数并像这样直接调用它(此博客发布)

On the other hand, while using subclassing sklearn.base.TransformerMixin you will have to define the whole class along with the fit and transform functions of the class. So you will have to create a class like this(Example code take from this blog post)

class FunctionFeaturizer(TransformerMixin):
    def __init__(self, *featurizers):
        self.featurizers = featurizers

    def fit(self, X, y=None):
        return self

    def transform(self, X):
        #Do transformations and return
        return transformed_data

如您所见,与FunctionTransformer相比, TransformerMixin 在转换功能方面为您提供了更大的灵活性.您可以根据值应用多个转换或部分转换,等等.例如,对于要记录的前50个值,而对于随后的50个值,则希望取反对数,依此类推.您可以轻松定义自己的转换方法,以选择性地处理数据.

So as you can see, TransformerMixin gives you more flexibility as compared to FunctionTransformer with regard to transform function. You can apply multiple trasnformations, or partial transformation depending on the value, etc. An example can be like, for the first 50 values you want to log while for the next 50 values you wish to take inverse log and so on. You can easily define your transform method to deal with data selectively.

如果您只是想直接使用函数,请使用 sklearn.preprocessing.FunctionTrasformer ,否则,如果您想进行更多修改或说复杂的转换,建议使用子类化sklearn.base.TransformerMixin

If you just want to directly use a function as it is, use sklearn.preprocessing.FunctionTrasformer, else if you want to do more modification or say complex transformations, I would suggest subclassing sklearn.base.TransformerMixin

在这里,请看以下链接以获得更好的主意

Here, take a look at the following links to get a more better idea

这篇好文章是转载于:学新通技术网

  • 版权申明: 本站部分内容来自互联网,仅供学习及演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,请提供相关证据及您的身份证明,我们将在收到邮件后48小时内删除。
  • 本站站名: 学新通技术网
  • 本文地址: /reply/detail/tanhcfjecj
系列文章
更多 icon
同类精品
更多 icon
继续加载