Skip to content

Pipeline requires both fit and transform method to be available instead of only fit_transform #16710

Done

Description

@jnboehm

Describe the bug

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.

Steps/Code to Reproduce

Example:

from sklearn.decomposition import PCA
from sklearn.manifold import TSNE
from sklearn.pipeline import make_pipeline

make_pipeline(TSNE(), PCA())

Expected Results

A pipeline is created.

Actual Results

Output:

TypeError: All intermediate steps should be transformers and implement fit and transform or be the string 'passthrough' 'TSNE(angle=0.5,...

Possible Solution

Editing this https://github.com/scikit-learn/scikit-learn/blob/95d4f0841/sklearn/pipeline.py#L179
to the following in order to reflect the change:

if (not hasattr(t, "fit_transform")) or not (hasattr(t, "fit") and hasattr(t, "transform")):

Versions

import sklearn; sklearn.show_versions()
System:
    python: 3.8.2 (default, Feb 26 2020, 22:21:03)  [GCC 9.2.1 20200130]
executable: /usr/bin/python3
   machine: Linux-5.5.9-arch1-2-x86_64-with-glibc2.2.5

Python dependencies:
       pip: 20.0.2
setuptools: 46.0.0
   sklearn: 0.22.2.post1
     numpy: 1.18.1
     scipy: 1.4.1
    Cython: 0.29.15
    pandas: 1.0.1
matplotlib: 3.2.0
    joblib: 0.14.1

Built with OpenMP: True

Activity

  1. jnothman commented on Mar 16, 2020

    @jnothman
    Member

    Yes, tsne, at least in our conception and implementation of it, does not induce a model that can be applied to a test set..

  2. jnothman commented on Mar 16, 2020

    @jnothman
    Member

    Tsne is transductive only

  3. jnboehm commented on Mar 16, 2020

    @jnboehm
    ContributorAuthor

    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.

  4. jnothman commented on Mar 17, 2020

    @jnothman
    Member

    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!

  5. added
    Low Priority
    Easy
    and removed on Mar 17, 2020
  6. jnboehm commented on Mar 17, 2020

    @jnboehm
    ContributorAuthor

    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)?

  7. johannfaouzi commented on Mar 17, 2020

    @johannfaouzi
    Contributor

    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_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).

  8. jnothman commented on Mar 17, 2020

    @jnothman
    Member
  9. jnboehm commented on Mar 17, 2020

    @jnboehm
    ContributorAuthor

    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.

  10. jnothman commented on Mar 17, 2020

    @jnothman
    Member
  11. nachidave commented on May 6, 2020

    @nachidave

    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

  12. removed
    Easy
    on Mar 1, 2022
  13. avm19 commented on Sep 25, 2025

    @avm19
    Contributor

    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).

  14. lorentzenchr commented on Nov 30, 2026

    @lorentzenchr
    Member

    I actually like the explicit error that the pipelibe raises.
    For the use cases describes here a simple

    class NewTSNE(TSNE):
        def transform(X):
            return X

    should do.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    BugLow Prioritymodule:pipeline

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions