WRITE · TRACE · UNDERSTAND

Project: train a stacking model from out-of-fold base predictions

Training rows have disjoint unique id, fold, finite scalar x,y; test rows need id,x only.

Read lesson: Project: train a stacking model from out-of-fold base predictions →

Your experiment

Training rows have disjoint unique id, fold, finite scalar x,y; test rows need id,x only. Require >=4 training rows, >=2 folds and >=2 fitting rows per held fold. Base learners are centered raw polynomial ridge models of degree 1 and 2, penalty .1 and unpenalized intercept. Form two OOF features per training row using only other folds. Fit a centered two-feature ridge meta-model with penalty .001 on OOF features. Refit bases on all training rows for test prediction. Return oof, folds with fit_ids/held_ids, meta weights,bias and predictions.

Python runs in your browser. Your draft stays on this device.

What to look for

Audit that every OOF feature excludes its target row's fold. Use an outer evaluation loop for the complete stack and defend fold construction before trusting an ensemble score. Replay follows the main orchestration function; helper computations execute and are checked separately.

Watch your code work

Ready to experiment

Write a solution or load the worked answer, then choose Trace & check.

Step through arrays, variables and training curves produced by your own Python.

About this algorithm

How Project: train a stacking model from out-of-fold base predictions works

The idea

Stacking combines several models by training a second model, the meta-model, on their predictions. The catch is what the meta-model trains on. A base model's predictions for rows it was fitted on are too good, so a meta-model trained on them learns to trust whichever base model overfits most.

stacked_regression(rows, test) avoids that with out-of-fold predictions. For each fold, both base learners (ridge on x, and ridge on x and its square) are fitted on the other folds and predict the held rows. Every training row ends up with two predictions from models that never saw its target. A small ridge meta-model learns weights for those two columns, and the base learners are then refitted on all rows to predict test.

How it works

  1. Validate the input: at least four training rows, at least two folds, unique IDs across training and test, finite values.
  2. For each fold, fit the degree 1 and degree 2 ridge models on the rows of all other folds.
  3. Predict the held fold's rows with both models and store the pair in oof under each row ID.
  4. Fit the meta-model, a centred two-feature ridge with penalty 0.001, on the out-of-fold pairs against y.
  5. Refit both base learners on all the training rows.
  6. For each test row, combine the two base predictions using the meta weights and bias.

What it costs

Base fits
2·folds + 2
Per fit
O(n)
Memory
O(n) for oof

Each of the two base learners is fitted once per fold to make the out-of-fold features and once more on all rows, and each fit is linear in the number of rows.

Where it is used

  • scikit-learn's StackingRegressor and StackingClassifier train the final estimator on cross-validated predictions of the base estimators.
  • The winning Netflix Prize team in 2009 blended hundreds of predictors, fitting the blend on ratings held out from base model training.
  • AutoGluon builds its tabular predictors with multi-layer stack ensembling on out-of-fold predictions.

Watch out for

  • Training the meta-model on in-sample base predictions, which rewards the base model that memorised the training rows.
  • Using test rows before the final prediction, for example to centre features or to choose the meta penalty.
  • Reading the meta weights as model importance: correlated base predictions can receive large weights of opposite sign.