Course contentsShow
← Machine Learning and Deep Learning
Lesson 183 of 3,5382. Machine Learning Mindset and Core ConceptsFree lesson

Cross-Validation with cross_val_score

Performing k-fold cross-validation to get robust performance estimates beyond a single train-test split.

Cross-Validation with cross_val_score

What you'll learn: How to get a more reliable estimate of your model's performance by testing it multiple times on different data splits.

Why a Single Train-Test Split Isn't Enough

You've already learned about splitting data into training and test sets. But here's the problem: one split gives you one performance number. What if you got lucky (or unlucky) with that particular split? Maybe your test set happened to contain unusually easy or hard examples.

Think of it like judging a restaurant by one meal—you might catch them on a great day or a terrible day. You'd get a better sense by visiting multiple times.

Enter k-Fold Cross-Validation

Cross-validation solves this by splitting your data into k parts (called "folds"), then training and testing k times. Each time, a different fold serves as the test set while the remaining folds train the model.

For example, with 5-fold cross-validation:

  • Round 1: Train on folds 2-5, test on fold 1
  • Round 2: Train on folds 1,3-5, test on fold 2
  • ...and so on

You get 5 performance scores, then average them for a robust estimate.

Using cross_val_score

Scikit-learn makes this trivial:

from sklearn.model_selection import cross_val_score
from sklearn.linear_model import LogisticRegression

model = LogisticRegression()
scores = cross_val_score(model, X, y, cv=5)

print(f"Scores: {scores}")
print(f"Mean accuracy: {scores.mean():.3f}")

The cv=5 parameter specifies 5-fold cross-validation. The function returns an array of scores—one per fold.

Key Takeaway: Cross-validation gives you multiple performance estimates instead of one, revealing how consistently your model performs and reducing the risk of misleading results from a lucky/unlucky split.