Build a regularized regression pipeline
End-to-End Signup Prediction with scikit-learn
Context
You are provided with a cleaned tabular dataset containing marketing and product metrics. Your objective is to develop a robust, leakage-free modeling workflow using scikit-learn to predict daily signups.
Assume the dataset is a pandas DataFrame that includes at least the following columns:
spend(float)clicks(float)cpc(float)region(categorical string)date(datetime or string parsable to datetime)signups(non-negative integer/float target)
Task
Build a machine learning model to predict signups using spend, clicks, cpc, region, and a time trend derived from the date column. Implement the following steps within scikit-learn:
- ColumnTransformer Setup
- Numeric features:
spend,clicks,cpc, and atime_index(extracted fromdate). ApplyPolynomialFeatures(degree=2, include_bias=False)followed byStandardScaler(). - Categorical feature:
region. ApplyOneHotEncoder(handle_unknown='ignore').
- Numeric features:
- Target Transformation Decision
- Perform a quick diagnostic (e.g., via cross-validated RMSE comparison on a simple baseline model) to determine whether to apply
log1pto the target variable. Iflog1pis selected, useTransformedTargetRegressor.
- Perform a quick diagnostic (e.g., via cross-validated RMSE comparison on a simple baseline model) to determine whether to apply
- Model Selection and Tuning
- Construct a
Pipelinethat encapsulates all preprocessing steps and a linear penalized model. - Tune
Ridge,Lasso, andElasticNetmodels usingGridSearchCVwith 5-fold cross-validation by default. - If temporal dependence is detected in the data, switch both the inner and outer cross-validation strategies to
TimeSeriesSplit.
- Construct a
- Evaluation and Leakage Control
- Create a held-out test split before any cross-validation or preprocessing. Use a chronological split if temporal dependence is detected; otherwise, use a random 80/20 split.
- Ensure that all preprocessing steps (including the creation of
time_index) are strictly contained within thePipelineto prevent data leakage. - Report RMSE as the primary evaluation metric, and also report R^2 on the held-out test set.
- Utilize nested cross-validation to obtain a robust estimate of the model's generalization RMSE.
- Feature Effects Analysis
- After selecting the best model, extract and rank the top 10 features based on their absolute standardized effect size.
- Map one-hot encoded feature names back to a human-readable format (e.g.,
region=NA, clear interaction terms).
- Model Persistence
- Provide code to save the trained model to disk and demonstrate how to reload it and use it to score new data.
- Rationale
- Offer a concise justification for each significant modeling choice made throughout the process.
AI interview prep
Core dilemma
Building a robust, production-ready machine learning pipeline that strictly adheres to best practices for preventing data leakage and ensuring reliable evaluation, particularly in the presence of temporal dependencies and complex feature engineering.
3 tradeoffs and 3 stress tests locked
Interview prep
Unlock the interviewer X-Ray rubric, tradeoff matrix, stress tests, clarifying questions, and key concepts.
Build a regularized regression pipeline
End-to-End Signup Prediction with scikit-learn
Context
You are provided with a cleaned tabular dataset containing marketing and product metrics. Your objective is to develop a robust, leakage-free modeling workflow using scikit-learn to predict daily signups.
Assume the dataset is a pandas DataFrame that includes at least the following columns:
spend(float)clicks(float)cpc(float)region(categorical string)date(datetime or string parsable to datetime)signups(non-negative integer/float target)
Task
Build a machine learning model to predict signups using spend, clicks, cpc, region, and a time trend derived from the date column. Implement the following steps within scikit-learn:
- ColumnTransformer Setup
- Numeric features:
spend,clicks,cpc, and atime_index(extracted fromdate). ApplyPolynomialFeatures(degree=2, include_bias=False)followed byStandardScaler(). - Categorical feature:
region. ApplyOneHotEncoder(handle_unknown='ignore').
- Numeric features:
- Target Transformation Decision
- Perform a quick diagnostic (e.g., via cross-validated RMSE comparison on a simple baseline model) to determine whether to apply
log1pto the target variable. Iflog1pis selected, useTransformedTargetRegressor.
- Perform a quick diagnostic (e.g., via cross-validated RMSE comparison on a simple baseline model) to determine whether to apply
- Model Selection and Tuning
- Construct a
Pipelinethat encapsulates all preprocessing steps and a linear penalized model. - Tune
Ridge,Lasso, andElasticNetmodels usingGridSearchCVwith 5-fold cross-validation by default. - If temporal dependence is detected in the data, switch both the inner and outer cross-validation strategies to
TimeSeriesSplit.
- Construct a
- Evaluation and Leakage Control
- Create a held-out test split before any cross-validation or preprocessing. Use a chronological split if temporal dependence is detected; otherwise, use a random 80/20 split.
- Ensure that all preprocessing steps (including the creation of
time_index) are strictly contained within thePipelineto prevent data leakage. - Report RMSE as the primary evaluation metric, and also report R^2 on the held-out test set.
- Utilize nested cross-validation to obtain a robust estimate of the model's generalization RMSE.
- Feature Effects Analysis
- After selecting the best model, extract and rank the top 10 features based on their absolute standardized effect size.
- Map one-hot encoded feature names back to a human-readable format (e.g.,
region=NA, clear interaction terms).
- Model Persistence
- Provide code to save the trained model to disk and demonstrate how to reload it and use it to score new data.
- Rationale
- Offer a concise justification for each significant modeling choice made throughout the process.