[scikit-learn] Strange code but that works
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:
> This is a code from sklearn.pipeline.Pipeline:
> def transform(self):
> """Apply transforms, and transform with the final estimator
> This also works where final estimator is ``None``: all prior
> transformations are applied.
> X : iterable
> Data to transform. Must fulfill input requirements of first step
> of the pipeline.
> 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:
> 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?
> Louis Abraham
> scikit-learn mailing list
> scikit-learn at python.org
INRIA Saclay - Parietal team
Center for Data Science Paris-Saclay
-------------- next part --------------
An HTML attachment was scrubbed...
More information about the scikit-learn