Build a leak-free sklearn pipeline
Boston Consulting GroupData Scientist · Online AssessmentLast updated
This problem tests the ability to build a robust scikit-learn pipeline that prevents data leakage during preprocessing and hyperparameter tuning while handling class imbalance.
The Task
You are tasked with building a machine learning pipeline to predict customer churn based on a dataset containing numerical features (e.g., account balance) and categorical features (e.g., region). The target variable is imbalanced (only 5% churn).
Construct a scikit-learn Pipeline that performs the following steps in order:
- Preprocessing: Apply
StandardScalerto numerical columns andOneHotEncoderto categorical columns usingColumnTransformer. - Resampling: Use
SMOTE(fromimblearn) to handle class imbalance, ensuring it only triggers during training. - Modeling: Use a
RandomForestClassifier.
Finally, wrap this pipeline in a GridSearchCV object to tune max_depth for the forest and evaluate performance using StratifiedKFold (5 splits) with f1_macro as the scoring metric.
Requirements
- Ensure the implementation is immune to data leakage (the scaler and resampler must not be fit on validation folds).
- Use
imblearn.pipeline.Pipelineinstead ofsklearn.pipeline.Pipelineto correctly handle resampling steps.
Example Workflow
Input: A pandas DataFrame df with X features and y target.
Output: A trained GridSearchCV object.
from imblearn.pipeline import Pipeline
from sklearn.compose import ColumnTransformer
# ... imports
def build_pipeline(num_cols, cat_cols):
# Define your transformers, pipeline, and grid search here
pass
Edge Cases to Consider
- How do you handle unknown categories in the validation set during
OneHotEncodertransformation? - Why is it critical to use
imblearn's Pipeline class instead of the standardsklearnversion when usingSMOTE?
Hints
Hint 1
Standard scikit-learn pipelines do not allow for steps that change the number of samples, so use the pipeline module from the imbalanced-learn library.
Hint 2
Data leakage is avoided when you fit your transformers and resamplers strictly within the cross-validation loop, which `GridSearchCV` does automatically when given a `Pipeline` object.
Hint 3
Ensure `handle_unknown='ignore'` is set in your `OneHotEncoder` to prevent the pipeline from crashing on categories not seen during training.
Follow-up questions
- How would you modify this pipeline if you needed to perform feature selection (e.g., SelectKBest) after the imputation/scaling steps?
- If the model is deployed in a real-time production environment, how does the `Pipeline` object simplify the inference process compared to manual preprocessing?
- Why is `f1_macro` or `balanced_accuracy` preferred over standard `accuracy` when dealing with the imbalanced dataset described?
Solution walkthrough is for members
Get the full walkthrough with reference code and complexity analysis, plus Kitty Coach at 300 messages a day. Hints, the code runner and community answers stay free.