本文介绍了Sklearn自定义转换器:使用FunctionTransformer和子类化TransformerMixin之间的区别的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为了执行适当的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?

推荐答案

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

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 时,您可以简单地定义要使用的函数并像这样直接调用它(官方文档中的代码)

For instance, while using sklearn.preprocessing.FunctionTransformer you can simply define the function you want to use and call it directly like this (code from official documentation)

def all_but_first_column(X):
    return X[:, 1:]

def drop_first_component(X, y):
    """
    Create a pipeline with PCA and the column selector and use it to
    transform the dataset.
    """
    pipeline = make_pipeline(PCA(), FunctionTransformer(all_but_first_column),)

    X_train, X_test, y_train, y_test = train_test_split(X, y)
    pipeline.fit(X_train, y_train)
    return pipeline.transform(X_test), y_test

另一方面,在使用子类化sklearn.base.TransformerMixin 时,您必须定义整个类以及 fit transform 类的功能.因此,您将必须创建这样的类(示例代码摘自此博客发布)

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

这篇关于Sklearn自定义转换器:使用FunctionTransformer和子类化TransformerMixin之间的区别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-21 07:27