Thuta Learning
IntermediateData & Databasesintermediate

Cross-Validation

Relax. We'll talk through this in plain words — no textbook voice.

What you'll walk away with

  • Understand Cross-Validation without being intimidated by it
  • Be able to run scikit-learn code yourself
  • Apply this concept in a real project right away

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

python
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})")
You should see
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.

Easy traps

  • Forgetting to retrain the final model on all the data after running Cross-Validation — CV only gives you a 'reliability estimate' of the model; the final deployment model needs to be trained on all the data
  • Setting K too large (K=50) — each fold ends up with too little data, and training time takes K times as long

Now Try It Yourself

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.

You'll know it worked when: Scores per fold: [0.84 0.87 0.82 0.86 0.85] Mean accuracy: 0.85 (+/- 0.02)

Cross-Validation | Thuta Learning