Passthrough Stacking Regressor with ElasticNet Meta-Model

Quick Overview

Implement a stacking regressor that builds out-of-fold base predictions from given fold labels, optionally appends the raw features, and fits a scaled ElasticNet meta-model. It tests leakage-free stacking, a consistent column layout between fit and predict, input validation, and parity with scikit-learn's StackingRegressor.

Passthrough Stacking Regressor with ElasticNet Meta-Model

Role: Machine Learning Engineer

Category: Machine Learning

Difficulty: hard

Interview Round: Other

Implement `PassthroughStackingRegressor`. `estimators` is a list of `(name, regressor)` pairs, and `folds` assigns a fold label to each training row. `fit` builds out-of-fold base predictions, optionally appends the original features, and trains a scaled `ElasticNet` meta-model. `transform` and `predict` reproduce the same column layout. ```python class PassthroughStackingRegressor: def __init__(self, estimators, folds, alpha=0.1, l1_ratio=0.5, passthrough=True, max_iter=10000): ... def fit(self, X, y): ... def transform(self, X): ... def predict(self, X): ... ``` Requirements: - `oof_predictions_` has shape `(n, m)` in estimator order. For each fold label `k`, fit a `clone` on rows where `folds != k` and predict the rows where `folds == k`. - `meta_features_` is `[oof | X]` when `passthrough=True` (predictions first), otherwise only `oof`. - `estimators_` holds clones refit on all rows (the originals stay unfitted). `transform(X)` stacks their predictions using the same layout. - `meta_model_` is a `Pipeline` of `StandardScaler` followed by `ElasticNet(alpha, l1_ratio, max_iter)`. `predict(X)` equals `meta_model_.predict(transform(X))`. - A wrong feature count at `transform` or `predict` raises `ValueError`. In `fit`, fewer than 2 folds, no estimators, or length mismatches raise `ValueError`. Example: ```python X = np.arange(6.).reshape(-1, 1); y = np.arange(1, 7.) folds = np.array([2, 0, 1, 0, 2, 1]) m = PassthroughStackingRegressor([("d", DummyRegressor())], folds).fit(X, y) m.oof_predictions_[:, 0] # [3.75, 3.75, 3.0, 3.75, 3.75, 3.0] ``` Fold 0 holds rows 1 and 3, so the dummy model for that fold is trained on the targets 1, 3, 5 and 6 and predicts their mean, 3.75. ```hint Column layout Write one helper that turns a block of base predictions plus `X` into meta-features, and call it from both `fit` and `transform`. ``` ```hint Two sets of base models The out-of-fold columns must come from models that never saw the rows they predict, while `transform` uses models fit on every row. Keep the two sets separate, and check what happens to the objects passed in `estimators`. ``` ### Constraints & Assumptions - scikit-learn is available. Base regressors follow its `fit`/`predict` API and can be copied with `sklearn.base.clone`. - `X` is a 2-D numeric array of shape `(n, d)`, `y` has length `n`, and `folds` has one label per training row. Each distinct label defines one held-out fold. - Results must match `sklearn.ensemble.StackingRegressor` with the same splits, `passthrough=True` and the same scaled `ElasticNet` final estimator. ### What a Strong Answer Covers - Builds each out-of-fold column from fresh clones trained without the held-out fold, in estimator order. - Keeps the meta-feature column order identical between `fit` and `transform`, with and without passthrough. - Refits clones on all rows for prediction while leaving the original estimators unfitted. - Validates inputs in `fit`, `transform` and `predict` and raises `ValueError` in each listed case. - States the training cost in base-model fits and how to check the result against `StackingRegressor`. ### Follow-up Questions - What goes wrong if the meta-model trains on in-sample base predictions? - How would you handle grouped or time-ordered data when building `folds`? - Why scale the meta-features before the `ElasticNet` when passthrough is on?

Overview: Implement a stacking regressor that builds out-of-fold base predictions from given fold labels, optionally appends the raw features, and fits a scaled ElasticNet meta-model. It tests leakage-free stacking, a consistent column layout between fit and predict, input validation, and parity with scikit-learn's StackingRegressor.

General logo
General
Nov 24, 2026
hardMachine Learning EngineerOtherMachine Learning
0
0

Implement PassthroughStackingRegressor. estimators is a list of (name, regressor) pairs, and folds assigns a fold label to each training row. fit builds out-of-fold base predictions, optionally appends the original features, and trains a scaled ElasticNet meta-model. transform and predict reproduce the same column layout.

class PassthroughStackingRegressor:
    def __init__(self, estimators, folds, alpha=0.1, l1_ratio=0.5,
                 passthrough=True, max_iter=10000): ...
    def fit(self, X, y): ...
    def transform(self, X): ...
    def predict(self, X): ...

Requirements:

  • oof_predictions_ has shape (n, m) in estimator order. For each fold label k , fit a clone on rows where folds != k and predict the rows where folds == k .
  • meta_features_ is [oof | X] when passthrough=True (predictions first), otherwise only oof .
  • estimators_ holds clones refit on all rows (the originals stay unfitted). transform(X) stacks their predictions using the same layout.
  • meta_model_ is a Pipeline of StandardScaler followed by ElasticNet(alpha, l1_ratio, max_iter) . predict(X) equals meta_model_.predict(transform(X)) .
  • A wrong feature count at transform or predict raises ValueError . In fit , fewer than 2 folds, no estimators, or length mismatches raise ValueError .

Example:

X = np.arange(6.).reshape(-1, 1); y = np.arange(1, 7.)
folds = np.array([2, 0, 1, 0, 2, 1])
m = PassthroughStackingRegressor([("d", DummyRegressor())], folds).fit(X, y)
m.oof_predictions_[:, 0]  # [3.75, 3.75, 3.0, 3.75, 3.75, 3.0]

Fold 0 holds rows 1 and 3, so the dummy model for that fold is trained on the targets 1, 3, 5 and 6 and predicts their mean, 3.75.

Constraints & Assumptions

  • scikit-learn is available. Base regressors follow its fit / predict API and can be copied with sklearn.base.clone .
  • X is a 2-D numeric array of shape (n, d) , y has length n , and folds has one label per training row. Each distinct label defines one held-out fold.
  • Results must match sklearn.ensemble.StackingRegressor with the same splits, passthrough=True and the same scaled ElasticNet final estimator.

What a Strong Answer Covers Guidance

  • Builds each out-of-fold column from fresh clones trained without the held-out fold, in estimator order.
  • Keeps the meta-feature column order identical between fit and transform , with and without passthrough.
  • Refits clones on all rows for prediction while leaving the original estimators unfitted.
  • Validates inputs in fit , transform and predict and raises ValueError in each listed case.
  • States the training cost in base-model fits and how to check the result against StackingRegressor .

Follow-up Questions Guidance

  • What goes wrong if the meta-model trains on in-sample base predictions?
  • How would you handle grouped or time-ordered data when building folds ?
  • Why scale the meta-features before the ElasticNet when passthrough is on?