Practice in Lab: Project: train a stacking model from out-of-fold base predictions →
Project: train a stacking model from out-of-fold base predictions
A stacked learner can silently train on unrealistically good in-sample base predictions. Its meta-model must see the kind of prediction available for unseen examples. This project builds out-of-fold features with real linear and quadratic ridge models, trains a second regression and refits the base learners for inference. Test inputs deliberately omit targets. The orchestration trace shows fold boundaries and the assembled OOF matrix; helper fits execute normally. This fixture does not choose the fold scheme or estimate the stack's generalization error.
folds = ['a','a','b','b']
oof = {}
for fold in sorted(set(folds)):
fitting = [i for i,g in enumerate(folds) if g!=fold]
for i,g in enumerate(folds):
if g==fold: oof[i]=fitting
print(oof)Review the design
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.
Your task
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.
The explanation opens here once every check passes.