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

Gregory, Matthew matt.gregory at oregonstate.edu
Fri Sep 23 16:16:43 EDT 2022


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


More information about the scikit-learn mailing list