[scikit-learn] methods available from last estimator in pipeline

bmreiniger bmreiniger at yahoo.com
Sat Sep 24 10:10:34 EDT 2022


Matt would still need to transform the data using the rest of the pipeline first. There are multiple naming options for pipeline steps, but in this case indexing is nice:```pipe[-1].kneighbors(    pipe[:-1].transform(X))```The user guide quote should probably be updated: not every method carries over. In the source (https://github.com/scikit-learn/scikit-learn/blob/36958fb240fbe435673a9e3c52e769f01f36bec0/sklearn/pipeline.py#L426) you can see what does, indicated by the `@available_if(_final_estimator_has(...))` decorator. These are the same as those listed in the docs page (https://scikit-learn.org/stable/modules/generated/sklearn.pipeline.Pipeline.html#sklearn.pipeline.Pipeline.decision_function). Anything else you need to call from the last step itself.Best, Ben Reiniger
-------- Original message --------From: Sole Galli via scikit-learn <scikit-learn at python.org> Date: 9/24/22  5:56 AM  (GMT-06:00) To: Scikit-learn mailing list <scikit-learn at python.org> Cc: Sole Galli <solegalli at protonmail.com> Subject: Re: [scikit-learn] methods available from last estimator in pipeline Did you try:pipeline.named_steps["the_string_name_for_knn"].kneighbours?pipeline should be replaced by the name you gave to your pipeline and the string in named_steps is the name you have to the knn when setting the pipe.SoleSent with Proton Mail secure email.------- Original Message -------On Friday, September 23rd, 2022 at 10:16 PM, Gregory, Matthew <matt.gregory at oregonstate.edu> wrote:> Hi all,> > I have what is probably a silly question. I read this passage on [1]:> > """> The pipeline has all the methods that the last estimator in the pipeline has, i.e. if the last estimator is a classifier, the Pipeline can be used as a classifier. If the last estimator is a transformer, again, so is the pipeline.> """> > I'm trying to create a pipeline where my last estimator is a KNeighborsClassifier and, instead of predict(), I was hoping to use kneighbors(). But unfortunately, when in a pipeline, I'm getting this AttributeError:> > AttributeError: 'Pipeline' object has no attribute 'kneighbors'> > Is kneighbors() really available from the Pipeline? Or is there an alternative way to call an element in the Pipeline to use it? I tried "pipe[-1].kneighbors(X)", but that doesn't seem to be applying the earlier transforms in the pipeline.> > Thanks for any pointers,> matt> > [1] https://scikit-learn.org/stable/modules/compose.html> _______________________________________________> scikit-learn mailing list> scikit-learn at python.org> https://mail.python.org/mailman/listinfo/scikit-learn_______________________________________________scikit-learn mailing listscikit-learn at python.orghttps://mail.python.org/mailman/listinfo/scikit-learn
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://mail.python.org/pipermail/scikit-learn/attachments/20220924/d0998b34/attachment-0001.html>


More information about the scikit-learn mailing list