Skip to content Skip to sidebar Skip to footer

Use Pipeline With Custom Transformer In Scikit Learn

I tried to transform the column 'X' using values in column 'y' (this is a toy example, just to show using y for transformation) before fitted by the last linear regression estimato

Solution 1:

The following statement in TransformerMixin will execute ,We can see that transform function only need X parameter

self.fit(X, y, **fit_params).transform(X)

Solution 2:

As stated previously, the fit_transform method doesn't pass y off to transform. What I've done previously is implement my own fit_transform. Not your code, but here's an example I wrote recently:

classMultiColumnLabelEncoder:def__init__(self, *args, **kwargs):
        self.encoder = StandardLabelEncoder(*args, **kwargs)
    deffit(self, X, y=None):
        returnselfdeftransform(self,X):
        data = X.copy()
        for i in range(data.shape[1]):
            data[:, i] = LabelEncoder().fit_transform(data[:, i])
        return data
    deffit_transform(self, X, y=None):
        returnself.fit(X, y).transform(X)

There are other ways. You could have y as a class param and access it in the transform method.

Edit: I should note that you can pass y off to your version of transform. So:

deffit_transform(self, X, y=None):
    return self.fit(X, y).transform(X, y)

Post a Comment for "Use Pipeline With Custom Transformer In Scikit Learn"