Search code examples
pythonscikit-learnpipeline

Drop a step from a sklearn pipeline using the step name


How to remove a step from a sklearn pipeline using the step name?

By position I know that it can be done:

pipeline.steps.pop(n)

But with a very large pipeline, it can be difficult to find the position of the step you want to remove.


Solution

  • You can turn off a pipeline step by setting the estimator to None:

    pipe.set_params(step_name=None)
    

    If you really need to delete the step altogether, I don't think there's an sklearn-specific way to do that. You can just find the index of the step and then pop or del it, e.g.

    idx = [
        idx
        for idx, name in enumerate(pipe.named_steps.keys())
        if name == 'step_name'
    ][0]