You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Pipeline requires both fit and transform method to be available instead of only fit_transform #16710
Calling a pipeline with a nonparametric function causes an error since the function transform() is missing. The pipeline itself calls the function fit_transform() if it's present. For nonparametric functions (the most prominent being t-SNE) a regular transform() method does not exist since there is no projection or mapping that is learned. It could still be used for dimensionality reduction.
That's a fair point, I was trying to use it for a single pass pipeline, essentially. I see how the lack of an internal model could become an issue once test data is introduced. Anyways, thanks for the explanation.
Oh, I didn't note that you made no attempt to run any methods on the pipeline. I agree that it should be possible to construct the pipeline and run fit_transform on it, even if it is a somewhat degenerate pipeline. Thanks for reporting!
In that case, what should the behavior be? Should it check the transform property of the transformer and raise a TypeError before trying to call the function here? Or should it warn at construction time that only fit_transform is available (or that this pipeline can only be called once)?
fit_transform if the last step is a transformer (i.e. has a transform method),
fit_predict if the last step has a fit_predict method.
An error should probably be raised if the last step is not compatible (i.e. is a predictor without a fit_predict method).
Edit: TSNE could be the last estimator of the pipeline, so the check should be run for all the steps. It might a good idea to perform a check on all the estimators first, to see if there is an estimator with limitations (only fit_transform for any transformer), then perform a if else loop for both cases (we could keep the current code for one case, we raise a warning for the other case).
It should be fine to fit, but raise an error when transform or predict etc
are called. It might make it a bit of a nuisance for people who try putting
tsne in a pipeline and only get an error after fitting... So I wonder if
it's worth warning in the case that an intermediate step does not support
transform.
I personally would just opt to warn in case an intermediate step only supports fit_transform.
Is my assumption correct that the @if_delegate_has_method would take care of raising errors already? Then simply warning during construction and letting it crash if the user specifies an unsupported operation would in my opinion be sufficient. Or should more cases be handled after the warning?
Since fit_transform will be preferred over transform I'm not sure whether the code needs to be changed for that at all.
Also, in what way do validate_steps and validate_transformers differ? This is a bit unclear to me. For now I'll change both functions.
if_delegate_has_method does not check the intermediate estimators, only the
final one.
FeatureUnion._validate_transformers handles all the transformers
identically. Pipeline._validate_steps has to handle the last step
differently.
I have a transformer (inside a pipeline) that should apply in training phase to the train set only (because training requires cleaner data than prediction), but in prediction phase it should pass through. It's possible to imagine the opposite case, in which a transformer is "passthrough" in training, but transforms the dataset in prediction/test. In general, there could be a need for different behaviours in train/test stages.
Is it true that in training fit_transform() is always prioritised by Pipeline (over fit().transform()) and never called in test/prediction? If so, then one can rely on this to distinguish between learning and prediction, right? I couldn't find this in docs.
(Asking here, because closely related to the issue).
Activity
jnothman commentedon Mar 16, 2020
Yes, tsne, at least in our conception and implementation of it, does not induce a model that can be applied to a test set..
jnothman commentedon Mar 16, 2020
Tsne is transductive only
jnboehm commentedon Mar 16, 2020
That's a fair point, I was trying to use it for a single pass pipeline, essentially. I see how the lack of an internal model could become an issue once test data is introduced. Anyways, thanks for the explanation.
jnothman commentedon Mar 17, 2020
Oh, I didn't note that you made no attempt to run any methods on the pipeline. I agree that it should be possible to construct the pipeline and run fit_transform on it, even if it is a somewhat degenerate pipeline. Thanks for reporting!
jnboehm commentedon Mar 17, 2020
In that case, what should the behavior be? Should it check the
transformproperty of the transformer and raise aTypeErrorbefore trying to call the function here? Or should it warn at construction time that only fit_transform is available (or that this pipeline can only be called once)?johannfaouzi commentedon Mar 17, 2020
I would go for the latter. It's a degenerate pipeline but users would expect it to work.
It would require to change https://github.com/scikit-learn/scikit-learn/blob/95d4f0841/sklearn/pipeline.py#L179-L180 so that, if an intermediate step has only
fit_transform, the available methods for the pipeline are:fit(as always),fit_transformif the last step is a transformer (i.e. has atransformmethod),fit_predictif the last step has afit_predictmethod.An error should probably be raised if the last step is not compatible (i.e. is a predictor without a
fit_predictmethod).Edit: TSNE could be the last estimator of the pipeline, so the check should be run for all the steps. It might a good idea to perform a check on all the estimators first, to see if there is an estimator with limitations (only
fit_transformfor any transformer), then perform aif elseloop for both cases (we could keep the current code for one case, we raise a warning for the other case).jnothman commentedon Mar 17, 2020
jnboehm commentedon Mar 17, 2020
I personally would just opt to warn in case an intermediate step only supports
fit_transform.Is my assumption correct that the
@if_delegate_has_methodwould take care of raising errors already? Then simply warning during construction and letting it crash if the user specifies an unsupported operation would in my opinion be sufficient. Or should more cases be handled after the warning?Since
fit_transformwill be preferred overtransformI'm not sure whether the code needs to be changed for that at all.Also, in what way do
validate_stepsandvalidate_transformersdiffer? This is a bit unclear to me. For now I'll change both functions.jnothman commentedon Mar 17, 2020
nachidave commentedon May 6, 2020
Hi,
I am a beginner I also want to to contribute to open source so can you guys give me a suggestion please from where to start
avm19 commentedon Sep 25, 2025
I have a transformer (inside a pipeline) that should apply in training phase to the train set only (because training requires cleaner data than prediction), but in prediction phase it should pass through. It's possible to imagine the opposite case, in which a transformer is "passthrough" in training, but transforms the dataset in prediction/test. In general, there could be a need for different behaviours in train/test stages.
Is it true that in training
fit_transform()is always prioritised byPipeline(overfit().transform()) and never called in test/prediction? If so, then one can rely on this to distinguish between learning and prediction, right? I couldn't find this in docs.(Asking here, because closely related to the issue).
lorentzenchr commentedon Nov 30, 2026
I actually like the explicit error that the pipelibe raises.
For the use cases describes here a simple
should do.