Ben and Sole,

Thanks for your helpful suggestions and insight.  Ben, I appreciate the 
additional details on what methods carry over and the nice shorthand on how to 
apply in case they don’t carry over.

matt

From: bmreiniger <bmreini...@yahoo.com>
Sent: Saturday, September 24, 2022 7:11 AM
To: Sole Galli via scikit-learn <scikit-learn@python.org>
Cc: Sole Galli <solega...@protonmail.com>; Gregory, Matthew 
<matt.greg...@oregonstate.edu>
Subject: Re: [scikit-learn] methods available from last estimator in pipeline


[This email originated from outside of OSU. Use caution with links and 
attachments.]
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@python.org<mailto:scikit-learn@python.org>>
Date: 9/24/22 5:56 AM (GMT-06:00)
To: Scikit-learn mailing list 
<scikit-learn@python.org<mailto:scikit-learn@python.org>>
Cc: Sole Galli <solega...@protonmail.com<mailto:solega...@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.

Sole

Sent with Proton Mail secure email.

------- Original Message -------
On Friday, September 23rd, 2022 at 10:16 PM, Gregory, Matthew 
<matt.greg...@oregonstate.edu<mailto:matt.greg...@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@python.org<mailto:scikit-learn@python.org>
> https://mail.python.org/mailman/listinfo/scikit-learn
_______________________________________________
scikit-learn mailing list
scikit-learn@python.org<mailto:scikit-learn@python.org>
https://mail.python.org/mailman/listinfo/scikit-learn
_______________________________________________
scikit-learn mailing list
scikit-learn@python.org
https://mail.python.org/mailman/listinfo/scikit-learn

Reply via email to