Stacking trains several base models, then learns how to combine their predictions. The meta-learner must see predictions on data the base models did not train on, otherwise it learns to trust whichever model overfit the most.
Implement two functions.
oof_predictions(model_fn, X, y, n_splits=3) returns an array of length n with out-of-fold predictions. Split the rows into n_splits contiguous blocks with no shuffling. For each block, call model_fn(X_train, y_train, X_block) on the other blocks, and store the result at the block's positions.
stacking_weights(P, y) takes P of shape (n, m), where each column is one base model's out-of-fold predictions, and returns the least-squares weights w of length m with no intercept, so that P @ w best matches y.
2 ≤ n_splits ≤ n, otherwise raise ValueError.P must have as many rows as y, otherwise raise ValueError.Use np.array_split(np.arange(n), n_splits) to get the contiguous blocks.
np.linalg.lstsq(P, y, rcond=None)[0] gives the weights.
No results yet
Run your code to check it against the examples, or submit to run every test.