Cross-validation in scikit-learn
Answer: The simplest way to use cross-validation is to call the cross_val_score helper function on the estimator and the dataset. The performance measure reported by k-fold cross-validation is then the average of the values computed in the loop. (S1)
Splitting and scoring
- In scikit-learn a random split into training and test sets can be quickly computed with the train_test_split helper function. (S1)
- By default, the score computed at each CV iteration is the score method of the estimator. (S1)
- When cv is an integer, KFold or StratifiedKFold strategies are used by default, the latter being used if the estimator derives from ClassifierMixin. (S1)
- cross_validate returns a dict containing fit-times, score-times (and optionally training scores, fitted estimators, train-test split indices) in addition to the test score. (S1)
Beyond the helpers
- scikit-learn provides many other tools for model evaluation, in particular for cross-validation, beyond train_test_split. (S2)
- It is also possible to manually iterate over the folds, use different data splitting strategies, and use custom scoring functions. (S2)
- Using a pipeline for cross-validation and searching will largely keep you from the common pitfall of leaking test data into training. (S2)
Claims
- id: C1 claim: cross_val_score is the simplest way to cross-validate. source: S1 evidence: "The simplest way to use cross-validation is to call the"
- id: C2 claim: The default CV score is the estimator's score method. source: S1 evidence: "the score computed at each CV iteration is the score"
- id: C3 claim: cross_validate also returns fit and score times. source: S1 evidence: "It returns a dict containing fit-times, score-times"
- id: C4 claim: You can use custom scoring functions. source: S2 evidence: "and use custom scoring functions."
Sources
- [S1] 3.1. Cross-validation: evaluating estimator performance — scikit-learn 1.9.1 documentation. scikit-learn docs. https://scikit-learn.org/stable/modules/cross_validation.html . License: BSD-3-Clause (scikit-learn/scikit-learn COPYING; Copyright (c) 2007-2026 The scikit-learn developers; https://github.com/scikit-learn/scikit-learn/blob/main/COPYING). Accessed 2026-11-04.
- [S2] Getting Started — scikit-learn 1.9.1 documentation. scikit-learn docs. https://scikit-learn.org/stable/getting_started.html . License: BSD-3-Clause (scikit-learn/scikit-learn COPYING; Copyright (c) 2007-2026 The scikit-learn developers; https://github.com/scikit-learn/scikit-learn/blob/main/COPYING). Accessed 2026-11-04.
License: this note is CC BY 4.0 (https://creativecommons.org/licenses/by/4.0/). Summary by iinventedlight (AI-assisted).
License: CC-BY-4.0 · Summary by iinventedlight (AI-assisted). Sources: scikit-learn docs, 3.1. Cross-validation: evaluating estimator performance — scikit-learn 1.9.1 documentation, BSD-3-Clause (scikit-learn/scikit-learn COPYING; Copyright (c) 2007-2026 The scikit-learn developers; https://github.com/scikit-learn/scikit-learn/blob/main/COPYING); scikit-learn docs, Getting Started — scikit-learn 1.9.1 documentation, BSD-3-Clause (scikit-learn/scikit-learn COPYING; Copyright (c) 2007-2026 The scikit-learn developers; https://github.com/scikit-learn/scikit-learn/blob/main/COPYING).
Cite this
iinventedlight. 2026. "Cross-validation in scikit-learn." iinventedlight Library, iil:note:n47bzr1m, version of 2026-11-04. https://iinventedlight.com/library/notes/sklearn-cross-validation/. License: CC-BY-4.0.
@misc{iil_note_n47bzr1m,
author = {{iinventedlight}},
title = {{Cross-validation in scikit-learn}},
howpublished = {iinventedlight Library},
year = {2026},
date = {2026-11-04},
url = {https://iinventedlight.com/library/notes/sklearn-cross-validation/},
note = {Permanent ID iil:note:n47bzr1m. License: CC-BY-4.0.},
}
Spotted a mistake? Use the MCP feedback tool or email hello@iinventedlight.com.
For language models and agents: start at /llms.txt. This page in Markdown: /library/notes/sklearn-cross-validation.md.