Python Tutorial

Cross Validation

Estimate model performance more reliably by testing on every part of the data in turn.

Why Not Just One Split?

A single train/test split gives one score that depends on which rows landed in the test set — unlucky splits mislead you, especially on small datasets. Cross-validation rotates the test set across the whole dataset and averages the results, giving a more stable estimate plus a sense of its variability.

K-Fold Cross Validation

K-fold splits the data into k equal parts. Each part serves as the test set once while the other k−1 parts train the model. You get k scores to average.

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

X, y = load_iris(return_X_y=True)
model = LogisticRegression(max_iter=1000)

scores = cross_val_score(model, X, y, cv=5, scoring="accuracy")
print("fold scores:", scores.round(3))
print(f"mean: {scores.mean():.3f}  std: {scores.std():.3f}")

Report the mean ± standard deviation: a small std means the estimate is trustworthy.

Stratified K-Fold for Classification

For classification, StratifiedKFold keeps each class's proportion the same in every fold — the default for classifiers in cross_val_score. Explicit control:

from sklearn.model_selection import StratifiedKFold, cross_val_score

skf = StratifiedKFold(n_splits=5, shuffle=True, random_state=42)
scores = cross_val_score(model, X, y, cv=skf)
print(scores.mean().round(3))

Choosing a CV Strategy

StrategyUse when
KFoldGeneral regression, i.i.d. rows
StratifiedKFoldClassification, especially imbalanced classes
LeaveOneOutVery small datasets (k = n)
GroupKFoldRows share a group (same patient/user) that must not split across folds
TimeSeriesSplitTemporal data — always train on the past, test on the future

Never use plain KFold on time series: shuffling lets the model "see the future" and inflates scores. Use TimeSeriesSplit.

Preventing Leakage with Pipelines

Preprocessing must be re-fit inside every fold. Wrapping it in a pipeline makes cross-validation do this automatically.

from sklearn.pipeline import make_pipeline
from sklearn.preprocessing import StandardScaler

pipe = make_pipeline(StandardScaler(), LogisticRegression(max_iter=1000))
print(cross_val_score(pipe, X, y, cv=5).mean().round(3))

Nested Cross Validation

When you both tune hyperparameters and estimate performance, use nested CV: an inner loop selects hyperparameters, an outer loop measures generalization. This avoids the optimistic bias of tuning and testing on the same folds.

from sklearn.model_selection import GridSearchCV, cross_val_score

grid = GridSearchCV(LogisticRegression(max_iter=1000),
                    {"C": [0.1, 1, 10]}, cv=5)
nested = cross_val_score(grid, X, y, cv=5)   # outer loop
print("unbiased estimate:", nested.mean().round(3))

Best Practices

  • Prefer 5- or 10-fold CV over a single split for small/medium data.
  • Stratify for classification; respect groups and time order when present.
  • Always cross-validate the whole pipeline, not just the estimator.
  • Report mean and standard deviation, and use nested CV when tuning.

Try It Yourself

Exercise 1: Run 5-fold cross-validation for logistic regression on iris and print the mean score.

Show solution
from sklearn.datasets import load_iris
from sklearn.linear_model import LogisticRegression
from sklearn.model_selection import cross_val_score
X, y = load_iris(return_X_y=True)
s = cross_val_score(LogisticRegression(max_iter=1000), X, y, cv=5)
print(s.mean().round(3))

Exercise 2: Why is plain k-fold wrong for time-series data?

Show solution

Shuffling lets the model train on future data and test on the past, leaking information. Use TimeSeriesSplit instead.

📘 Real-World Deep Dive

Knowing <strong>ML Cross Validation (scikit-learn)</strong> well is what turns scikit-learn from a curiosity into a daily tool — you'll reach for it in nearly every real project.

Real-Life Scenario

An end-to-end usage of ML Cross Validation that you'd actually see in a data pipeline or analytics notebook.

Real-Life Example

import numpy as np
from sklearn.datasets import load_iris
from sklearn.model_selection import cross_val_score
from sklearn.linear_model import LogisticRegression
X, y = load_iris(return_X_y=True)
scores = cross_val_score(LogisticRegression(max_iter=200), X, y, cv=5)
print("mean:", round(scores.mean(), 3), "std:", round(scores.std(), 3))

Expected Output

(see source)

Common mistakes

  • fit expects numeric arrays; OneHotEncoder / LabelEncoder are easy to forget for categorical features.
  • Calling predict on a model trained on unscaled data and then scaling inputs at inference time silently degrades accuracy.
  • train_test_split(X, y) requires both arrays; train_test_split(X) for unsupervised learning slips past static checkers.
  • Treating ML Cross Validation as a black box without reading the docs — the API has subtle defaults that bite when you scale.

🚀 Performance & Best Practices

  • Wrap preprocessing + estimator in a Pipeline so fit / predict stay reproducible.
  • Use joblib / pickle for serialising models, not the entire Python state.
  • For > 100 k rows, switch to HistGradientBoostingClassifier or cuML.
  • When working with scikit-learn, prefer vectorised / batched operations over Python loops.

🧪 Try It Yourself

  1. Reproduce the snippet on a representative slice of your own data.
  2. Profile the snippet with cProfile or timeit and find the single biggest improvement.
  3. Generalise the snippet into a small, reusable function you can drop into future projects.

FAQ: Cross Validation

Common questions about this page.

What is Cross Validation?

Cross Validation is a Machine Learning lesson that explains cross validation in Python. Estimate model performance more reliably by testing on every part of the data in turn. Copy the samples and run them in the Python editor. It is written for beginners who want a clear definition and working examples.

Should I run cross validation examples locally for better learning?

Yes. Use the browser editor on StudyGrid for a quick check, then Download the example and run it on your computer. Local runs show real errors and the real toolchain, which is one of the fastest ways to learn cross validation in this Machine Learning Python lesson (Cross Validation).

How do I use cross validation in Python?

To use cross validation in Python, follow the examples on this StudyGrid page. Copy a snippet, run it in the browser, then Download and run it locally for better learning. Change the values and compare the output.

What is the syntax of cross validation?

This Cross Validation tutorial shows cross validation syntax with short Python examples. Use the code blocks in this lesson for the exact statements, then try them in your editor.

Cross Validation example for beginners

Yes. This page includes a beginner cross validation example you can copy and run. It is designed for searches such as "cross validation for beginners", "cross validation example", and "how to use cross validation".

What are common mistakes with cross validation?

Common cross validation mistakes include wrong syntax, mixing types, and skipping practice. Work through this Machine Learning chapter in order, run every example, and check the output before moving on.

Why should I learn cross validation?

Cross Validation is used in real Python work. Learning cross validation helps you write clearer programs and continue the Machine Learning tutorial on StudyGrid.

Is Cross Validation free to learn online?

Yes. You can learn cross validation free on StudyGrid (studygrid.in). This chapter is part of the Machine Learning path and includes examples, syntax, and next-step links.