วันจันทร์ที่ 17 สิงหาคม พ.ศ. 2569

Out-of-Fold (OOF)

Out-of-Fold (OOF) is a validation and model-building technique used heavily in machine learning (especially in competitions like Kaggle) to prevent data leakage and generate unbiased predictions on your training data.

To understand it, you first need to recall how K-Fold Cross-Validation works:
  1. You split your training data into $K$ parts (e.g., $K = 5$ folds).
  2. You train your model $5$ times. In each iteration, you use $4$ folds for training and leave $1$ fold out for validation.

How Out-of-Fold Predictions Work

An Out-of-Fold prediction is a prediction made on a validation fold by a model that was not trained on that fold.

If you do this for all $5$ folds:
  • Fold 1 is validated by a model trained on Folds 2, 3, 4, 5.
  • Fold 2 is validated by a model trained on Folds 1, 3, 4, 5.
  • (and so on...)

Once all $K$ iterations are finished, every single row in your original training dataset has been predicted exactly once by a model that had never seen it during training.

When you stitch all these predictions back together in the correct order, you get a full column of Out-of-Fold predictions spanning your entire training dataset.

Why is OOF Prediction Important?

1. Unbiased Evaluation of Model Performance

If you evaluate your model on data it was trained on, you get overoptimistic metrics due to overfitting. OOF metrics give you a realistic, honest estimate of how your model will perform on completely unseen test data.

2. Creating Meta-Features for Stacking (Blending)

This is the most powerful use case for OOF predictions.

  • If you want to build a stacked ensemble (where Model C takes the predictions of Model A and Model B as its inputs), you cannot just feed Model C the predictions your models made on the training data. If you do, Model C will overfit because Models A and B already "memorized" those training rows.
  • Instead, you feed Model C the OOF predictions of Model A and Model B. Because those OOF predictions were generated when each row was "out-of-fold," they mimic how the models behave on truly unseen data.

3. Threshold Tuning and Post-Processing

You can use your OOF predictions to tune decision thresholds (e.g., finding the optimal probability cutoff for classification) without risking data leakage, because those predictions were never biased by the training labels of those specific rows.