[scikit-learn] Strange code but that works

Louis Abraham louis.abraham at yahoo.fr
Sun Oct 28 02:40:36 EDT 2018


Hi,

This is a code from sklearn.pipeline.Pipeline:
    @property
    def transform(self):
        """Apply transforms, and transform with the final estimator

        This also works where final estimator is ``None``: all prior
        transformations are applied.

        Parameters
        ----------
        X : iterable
            Data to transform. Must fulfill input requirements of first step
            of the pipeline.

        Returns
        -------
        Xt : array-like, shape = [n_samples, n_transformed_features]
        """
        # _final_estimator is None or has transform, otherwise attribute error
        # XXX: Handling the None case means we can't use if_delegate_has_method
        if self._final_estimator is not None:
            self._final_estimator.transform
        return self._transform

I don't understand why `self._final_estimator.transform` can be returned, ignoring all the previous transformers.
However, when testing it works:

```
>>> p = make_pipeline(FunctionTransformer(lambda x: 2*x), FunctionTransformer(lambda x: x-1))
>>> p.transform(np.array([[1,2]]))
array([[1, 3]])
```

Could somebody explain that to me?

Best,
Louis Abraham

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/scikit-learn/attachments/20181028/6a0a2222/attachment.html>


More information about the scikit-learn mailing list