[scikit-learn] Strange code but that works

Guillaume Lemaître g.lemaitre58 at gmail.com
Sun Oct 28 05:44:54 EDT 2018


On Sun, 28 Oct 2018 at 07:42, Louis Abraham via scikit-learn <
scikit-learn at python.org> wrote:

> 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.
>

It is not returned. It is called such that if the final estimator does not
implement a transform method then it will raise an error.
Otherwise, _transform is called, which is actually perform all the
transform of all transformer (except the one that are set to None)
This is actually what the comment is referring to above (_final_estimator
is None or has transform, otherwise attribute error).



> 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
>
> _______________________________________________
> scikit-learn mailing list
> scikit-learn at python.org
> https://mail.python.org/mailman/listinfo/scikit-learn
>


-- 
Guillaume Lemaitre
INRIA Saclay - Parietal team
Center for Data Science Paris-Saclay
https://glemaitre.github.io/
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/scikit-learn/attachments/20181028/3fe99226/attachment.html>


More information about the scikit-learn mailing list