scikit-learn interview question · Most asked
How do scikit-learn Pipelines and ColumnTransformer work, and why use them?
How it works Asked in Machine Learning Engineer interviews
Summary
A Pipeline chains transformers and a final estimator so fit runs every step in order on the training data and
predict applies the same fitted steps to new data. ColumnTransformer applies different preprocessing to
different columns. Together they prevent leakage in cross-validation and give you one object to tune and deploy.
Longer Answer
- Hyperparameter search reaches inside with
step__paramnames:{"clf__max_depth": [3, 5]}. make_pipelineandmake_column_transformername steps automatically.set_output(transform="pandas")keeps column names through the pipeline for debugging.- Custom steps implement
fit/transform(or useFunctionTransformer). - Save the fitted pipeline with joblib so serving uses identical preprocessing.
Key Points
- Chains preprocessing and model.
- Different transforms per column.
- Prevents leakage in CV.
- One object to tune and deploy.
Explanation
What interviewers look for
Practical use including tuning through the pipeline and persistence.
More scikit-learn questions
- What do fit, transform, fit_transform and predict do in scikit-learn?
- How do you tune hyperparameters with GridSearchCV or RandomizedSearchCV?
- How do you handle categorical features in scikit-learn?
- Your scikit-learn model's cross-validation score varies a lot between folds. What does it mean, and what do you do?