Sub topic 7
Out-of-fold predictions
This chapter assumes you know k-fold cross-validation, the difference between training and test data, and basic scikit-learn usage. It explains how to build meta-features without leakage.
The problem to solve
A meta-learner needs training rows made of base-model predictions. If a base model predicts on the same rows it was trained on, its predictions look much better than they will on new data. A random forest, for example, often scores near 100% on its own training rows. The meta-learner then learns to trust that model too much.
An out-of-fold (OOF) prediction fixes this. It is a prediction for a row made by a model that never saw that row during training.
How OOF predictions are built
With 5-fold cross-validation, the training set is split into five parts. For each part:
- Train the base model on the other four parts.
- Predict on the held-out part.
- Store those predictions in the matching rows.
After five rounds, every training row has exactly one prediction, and each came from a model that did not see that row.
Doing it in scikit-learn
cross_val_predict performs this loop for you. The example below compares in-sample predictions with OOF predictions for one model.
from sklearn.datasets import make_classification
from sklearn.ensemble import RandomForestClassifier
from sklearn.metrics import log_loss
from sklearn.model_selection import cross_val_predict
X, y = make_classification(n_samples=2000, n_features=20, n_informative=8, random_state=0)
model = RandomForestClassifier(n_estimators=200, random_state=0)
in_sample = model.fit(X, y).predict_proba(X)
oof = cross_val_predict(model, X, y, cv=5, method="predict_proba")
print("in-sample log loss:", round(log_loss(y, in_sample), 3))
print("out-of-fold log loss:", round(log_loss(y, oof), 3))The in-sample loss is far lower, which is the overconfidence a meta-learner would absorb. The OOF loss is the honest figure.
Things to get right
- Use
method="predict_proba"when you want probabilities. The default returns class labels. For binary problems, keep one column ([:, 1]), because the two columns sum to 1 and carry the same information. - Stack one column per model. The meta-learner's input has shape (rows, models) for binary problems.
- Refit on all training data for test time. OOF predictions train the meta-learner. For the test set, each base model is refit on the full training set, and its predictions on test rows become the meta-learner's input.
- Use the same folds for every model if you want comparable meta-features. Set a seeded
KFoldorStratifiedKFoldand pass it ascv. - Keep preprocessing inside the fold. Put scalers in a pipeline with the model, so statistics come only from each fold's training part.
A small mismatch to know about
OOF models train on 80% of the data with 5 folds, while the refit models used at test time train on 100%. The test-time predictions are therefore slightly better than the OOF ones the meta-learner learned from. This gap is usually harmless. More folds shrink it but raise the cost.
Exercise
Why is it wrong to train the meta-learner on model.fit(X_train, y_train).predict_proba(X_train), and what do you use instead?
Show answer
The base model has seen those rows, so its predictions are overconfident compared with its predictions on new data, and the meta-learner learns to over-trust it. Use out-of-fold predictions from cross_val_predict(..., method="predict_proba"), where each row is predicted by a model that did not train on it.
Exercise
With 5-fold cross-validation, how many base-model fits happen per base model when building a stack for test-time use, and why?
Show answer
Six. Five fits produce the OOF predictions for the training rows. One more fit on the full training set produces the predictions for the test rows.