How k-fold cross-validation works

January 15, 2024

The diagram shows a five-fold validation run. kfold_cross_validation_diagram0alt Light blue marks the validation data and gray marks the training data. In k-fold cross-validation you train the model k times, holding out a different fold each time. The average validation score gives you an estimate of accuracy. A fold held out in one round takes part in training in other rounds. After evaluating all the folds, train the final model on the entire training and validation pool. That leaves you with both an accuracy estimate and a fitted model.

⚠️ Warning: Keep the test dataset out of this pool.

Here is another diagram I found helpful. kfold_cross_validation_diagram1alt

Further reading

  1. 一枚宅小宋 (June 4, 2019). Understanding and applying k-fold cross-validation. Zhihu. https://zhuanlan.zhihu.com/p/67986077
  2. 刘思聪 (October 23, 2018). Surviving Kaggle: The Amazon rainforest challenge. Zhihu. https://zhuanlan.zhihu.com/p/28084438

© 2024 borui. All rights reserved. This content may be freely reproduced, displayed, modified, or distributed with proper attribution to borui and a link to the article: borui (2024-01-15). How k-fold cross-validation works. https://caiborui.com/blog/2024-01-15-zh-kfold-validation-entrans

@misc{borui2024-e5adf777,
  author = {borui},
  title = {{How k-fold cross-validation works}},
  year = {2024},
  publisher = {borui's blog},
  url = {https://caiborui.com/blog/2024-01-15-zh-kfold-validation-entrans}
}