Let's Think About This for a Second
K-Fold Cross-Validation splits the data into K parts (folds) and runs K iterations — in each round, one fold is held out as the test set while the remaining (K-1) folds are used as training data — after repeating this K times, the average performance is taken as the final result — this is more reliable than a single train/test split (since every piece of data gets to be in both the training and test sets at some point).
Connecting to a Real Scenario
Writing `from sklearn.model_selection import cross_val_score; scores = cross_val_score(model, X, y, cv=5)` splits the data into 5 folds, trains/tests the model 5 times, and gives you 5 accuracy results in the `scores` array — looking at `scores.mean()` (average performance) and `scores.std()` (how consistent the results are, the standard deviation) helps you better understand the model's real-world reliability.
Let's Look at It Together
from sklearn.model_selection import cross_val_score
scores = cross_val_score(model, X, y, cv=5)
print(f"Scores per fold: {scores}")
print(f"Mean accuracy: {scores.mean():.2f} (+/- {scores.std():.2f})")Scores per fold: [0.84 0.87 0.82 0.86 0.85]
Mean accuracy: 0.85 (+/- 0.02)Try It in 5 Minutes
Evaluate a model (pick one from the Intermediate lesson) using `cross_val_score(cv=5)`, and compare the result from a single train/test split against the average result from cross-validation.
A Quick Word of Caution
If the standard deviation of the Cross-Validation results is large (meaning accuracy varies a lot from fold to fold), that's a signal the model is unstable or inconsistent — you should double-check the data quality or the model's complexity.