What Is Cross-Validation? k-Fold Explained Simply
Cross-validation tests a model on several splits of the data and averages the scores. See k-fold on a tiny worked example and run it in scikit-learn.
Short answer: Cross-validation is a way to test a machine learning model by training and scoring it several times on different splits of the same data, then averaging the scores. Every example is used for testing exactly once. The result is a more trustworthy estimate of performance on new data than a single train/test split gives.
What is cross-validation in machine learning?
Cross-validation in machine learning is a repeated testing method. A single split into training and validation data can be lucky or unlucky: if the easy examples happen to land in the validation part, the model looks better than it is. Cross-validation removes much of that luck by trying several splits and looking at all the results together.
How does k-fold cross-validation work?
k-fold cross-validation splits the data into k equal parts, called folds, and runs k rounds. In each round, one fold is held out for testing and the model is trained on the other k − 1 folds. After all rounds, you have k scores; their mean is the estimate and their spread shows how stable the model is. Common choices are k = 5 or k = 10.
What does k-fold look like on a tiny example?
Imagine 10 labelled examples, numbered 1 to 10, and k = 5. Each fold holds 2 examples. The correct-answer counts below are made up to show the arithmetic:
Round | Test fold | Train on | Correct in test fold | Accuracy |
|---|---|---|---|---|
1 | 1, 2 | 3–10 | 2 of 2 | 1.0 |
2 | 3, 4 | 1–2, 5–10 | 1 of 2 | 0.5 |
3 | 5, 6 | 1–4, 7–10 | 2 of 2 | 1.0 |
4 | 7, 8 | 1–6, 9–10 | 2 of 2 | 1.0 |
5 | 9, 10 | 1–8 | 1 of 2 | 0.5 |
The mean accuracy is (1.0 + 0.5 + 1.0 + 1.0 + 0.5) / 5 = 4.0 / 5 = 0.8. Notice that the scores jump between 0.5 and 1.0: with tiny folds, one example changes the score a lot. That spread is a warning sign you would never see from a single split.
How do you run cross-validation in Python?
You run cross-validation in Python with cross_val_score from scikit-learn. This example uses the iris dataset that ships with the library:
from sklearn.datasets import load_iris
from sklearn.linear_model import LogisticRegression
from sklearn.model_selection import cross_val_score
from sklearn.pipeline import make_pipeline
from sklearn.preprocessing import StandardScaler
X, y = load_iris(return_X_y=True)
# Scaling sits inside the pipeline, so it is re-fitted on each training fold
model = make_pipeline(StandardScaler(), LogisticRegression(max_iter=1000))
scores = cross_val_score(model, X, y, cv=5) # 5-fold cross-validation
print("Score for each fold:", scores)
print(f"Mean: {scores.mean():.3f} Std: {scores.std():.3f}")The script prints five scores, one per fold, then their mean and standard deviation. The exact numbers can vary with your scikit-learn version, so focus on the pattern: a high mean with a small spread is what you want. For classifiers, passing cv=5 uses stratified folds, which keep the share of each class similar in every fold. The scikit-learn cross-validation guide lists the other splitting options.
When should you use cross-validation?
Small or medium datasets, where one split leaves too little data to trust.
Comparing models or settings, so the winner is not just lucky on one split.
Checking for overfitting: a training score far above the cross-validation score is a red flag.
On very large datasets, or when training is slow, a single validation split is often enough. Cross-validation does not replace the final test set: choose your model with cross-validation, then measure it once on test data it has never seen.
Common mistakes
Data leakage: scaling or selecting features on all the data before splitting. Put those steps in a pipeline, as in the code above.
Shuffling time series: for data ordered in time, test on later periods only; scikit-learn has
TimeSeriesSplitfor this.Splitting related rows apart: if one person has many rows, keep them in the same fold (for example with
GroupKFold).Reporting only the mean: the spread tells you how far to trust it. See mean, variance and standard deviation.
Frequently Asked Questions
What is a good value of k in cross-validation?
Five or ten folds are common defaults and work well for most datasets. A larger k uses more data for training in each round but takes longer to run.
Is cross-validation the same as a validation set?
Both are used to judge a model before the final test. A validation set is one fixed split, while cross-validation rotates the validation role across every part of the data.
Does cross-validation prevent overfitting?
Cross-validation does not stop a model from overfitting, but it helps you detect it and choose settings that generalise better. Simpler models, more data and regularisation are what reduce overfitting.
What is leave-one-out cross-validation?
Leave-one-out is k-fold with k equal to the number of examples, so each round tests on a single example. It uses data efficiently but is slow on large datasets.
Key Takeaway
Cross-validation trains and tests a model k times on rotating folds and averages the scores. Use it to compare models fairly, read both the mean and the spread, keep preprocessing inside a pipeline, and still save a final test set.
Ready to start? You will learn the statistics behind model evaluation, from averages and spread to precision and recall. Begin with the free course Maths and Statistics for AI: The Essentials.
Related articles
More in Machine Learning
How Does Machine Learning Detect Fraud?
Machine learning fraud detection scores every transaction using learned fraud patterns and anomaly detection. See why 99% accuracy can catch nothing, a worked precision and recall example, and runnable code.
6 min readDec 2, 2026What Is Data Labeling and Why Does AI Need It?
Data labeling adds the right answers to raw data so models can learn. See the main label types, how to measure labeler agreement, and how AI-assisted labeling works.
5 min readNov 30, 2026What Is Feature Engineering? Better Data for Better Models
Feature engineering turns raw data into inputs a model can learn from. See the common techniques, a runnable Python example, and the mistakes that cause data leakage.
4 min readNov 26, 2026Gradient Descent Explained Simply
How models learn by measuring error and stepping downhill, with a runnable Python example showing learning rate, steps and local minima.
5 min readNov 23, 2026What Is Computer Vision? How AI “Sees” Images
Computer vision lets AI work with images and video. See how a picture becomes numbers, how models learn to recognise things, the main tasks, and where it goes wrong.
5 min readNov 22, 2026What Is NLP (Natural Language Processing)?
NLP is how computers read and produce human language. See how it works, the main tasks, a small Python example, and how NLP relates to LLMs.
4 min readNov 20, 2026
Comments
Sign in to join the discussion.

