scikit-learn
machine learning
data preprocessing
Python
pipeline customization

Is it possible to toggle a certain step in sklearn pipeline?

ML System Design practice on Codemia

Design recommenders, ranking systems and training pipelines the way ML interviews actually ask for them, with worked solutions.

Practice ML system design

Introduction

Yes. In scikit-learn, you can effectively toggle a pipeline step by replacing it with "passthrough" or by swapping it with another estimator during parameter search. That lets you compare preprocessing choices without rewriting the whole training script.

This is especially useful for ablation studies, hyperparameter tuning, and controlled experiments where you want the exact same cross-validation protocol with only one step enabled or disabled.

Use "passthrough" to Disable a Step

For a normal Pipeline, the standard toggle is to set a transformer step to "passthrough":

python
1from sklearn.pipeline import Pipeline
2from sklearn.preprocessing import StandardScaler
3from sklearn.decomposition import PCA
4from sklearn.linear_model import LogisticRegression
5
6pipe = Pipeline([
7    ("scale", StandardScaler()),
8    ("reduce", PCA(n_components=5)),
9    ("clf", LogisticRegression(max_iter=1000)),
10])
11
12pipe.set_params(reduce="passthrough")

The pipeline still has the same overall structure, but the reduce step now forwards the data unchanged.

This pattern becomes especially powerful in GridSearchCV:

python
1from sklearn.model_selection import GridSearchCV
2
3param_grid = {
4    "reduce": [PCA(n_components=5), "passthrough"],
5    "clf__C": [0.1, 1.0, 10.0],
6}
7
8grid = GridSearchCV(pipe, param_grid=param_grid, cv=5, n_jobs=-1)

Now the search compares both versions under the same evaluation procedure. That is much better than training separate scripts and hoping the results are comparable.

Toggle Parts of a ColumnTransformer

For tabular data, you often want to enable or disable one branch of preprocessing rather than a whole pipeline stage. ColumnTransformer supports the same idea:

python
1from sklearn.compose import ColumnTransformer
2from sklearn.preprocessing import OneHotEncoder, StandardScaler
3
4preprocess = ColumnTransformer(
5    transformers=[
6        ("num", StandardScaler(), ["age", "income"]),
7        ("cat", OneHotEncoder(handle_unknown="ignore"), ["city"]),
8    ],
9    remainder="drop",
10)
11
12pipe = Pipeline([
13    ("prep", preprocess),
14    ("clf", LogisticRegression(max_iter=1000)),
15])
16
17pipe.set_params(prep__num="passthrough")

Here, the numeric branch is disabled while the categorical branch stays active.

Remember that "drop" and "passthrough" mean different things. "drop" removes features completely. "passthrough" keeps them unchanged.

Use a Wrapper for Runtime-Controlled Toggles

If you want a more explicit on-off flag, you can wrap a transformer:

python
1from sklearn.base import BaseEstimator, TransformerMixin
2
3
4class OptionalTransformer(BaseEstimator, TransformerMixin):
5    def __init__(self, transformer=None, enabled=True):
6        self.transformer = transformer
7        self.enabled = enabled
8
9    def fit(self, X, y=None):
10        if self.enabled and self.transformer is not None:
11            self.transformer.fit(X, y)
12        return self
13
14    def transform(self, X):
15        if self.enabled and self.transformer is not None:
16            return self.transformer.transform(X)
17        return X

This is useful when a step needs a custom toggle behavior or when you want the flag itself to appear in the parameter search space.

Be Careful About Downstream Assumptions

Disabling a step changes the feature representation seen by downstream estimators. If a classifier expects scaled features and you turn off scaling, performance may drop sharply. If you toggle dimensionality reduction, the feature count changes, which can affect model selection and training time.

That is why toggle experiments should be tracked carefully. Save the best estimator and the full parameter set so the result is reproducible.

Common Pitfalls

The biggest mistake is leaving incompatible hyperparameters in the search space when a step is disabled. If a parameter only makes sense when PCA is active, keep the grid coherent.

Another common issue is confusing "drop" with "passthrough" in ColumnTransformer. One removes the branch entirely; the other keeps the raw features.

People also branch the training code manually with many if statements instead of using pipeline parameters. That makes experiments harder to reproduce and compare.

Finally, remember that disabling preprocessing is not a neutral change. It changes what the estimator sees, so interpret the results as a real model comparison.

Summary

  • Use "passthrough" to disable a scikit-learn pipeline step cleanly.
  • Compare enabled and disabled steps within one GridSearchCV run when possible.
  • Toggle ColumnTransformer branches the same way for tabular preprocessing.
  • Use a wrapper transformer when you need explicit runtime-controlled behavior.
  • Keep the parameter grid coherent and track the final pipeline configuration for reproducibility.

Related reading
Free course
Beginner
7 lessons
2 hours
Tackling System Design Interview Problems

A short course that equips you with the skills to approach system design interviews methodically.

Start the free course
Track what you have practised

A free account saves your progress, solutions and study plan across every problem on Codemia.

ML System Design practice on Codemia

Design recommenders, ranking systems and training pipelines the way ML interviews actually ask for them, with worked solutions.

Practice ML system design

All Rights Reserved.