Classical ML

Cross-Validation Done Right

Evaluating model generalization accuracy across K-Fold, Stratified, Group, and Time Series validation splits.

🟢 beginner5 min readevaluationmust-know
Cross Validation evaluates how a machine learning model generalizes to unseen test data by partitioning training datasets into multiple validation splits. Standard K-Fold splits data into K equal folds. Stratified K-Fold preserves class label proportions across folds, essential for imbalanced classification. Group K-Fold prevents data leakage by ensuring all samples from a specific subject or user stay in the same fold. Time Series Split respects temporal causality, using past data to predict future time steps.

Why Is Cross-Validation Necessary?

A single train-test split (e.g. 80% train, 20% test) is risky:

  1. High Variance: Test accuracy depends heavily on which specific samples landed in the 20% test bucket by random chance.
  2. Data Waste: 20% of your data is excluded from training.

Cross-Validation (CV) evaluates model stability by training and validating across multiple complementary data splits.

┌──────────────────────────┬──────────────────────────┬──────────────────────────┬──────────────────────────┐
│ 1. K-FOLD CV             │ 2. STRATIFIED K-FOLD     │ 3. GROUP K-FOLD          │ 4. TIME SERIES SPLIT     │
├──────────────────────────┼──────────────────────────┼──────────────────────────┼──────────────────────────┤
│ Splits data into K equal │ Preserves class label    │ Keeps all samples from   │ Expanding window that    │
│ folds. Trains K times.   │ proportions in every     │ the same group/user      │ respects temporal past-to│
│ Standard default.        │ fold. Imbalanced data!   │ in the same fold.        │ future causality.        │
└──────────────────────────┴──────────────────────────┴──────────────────────────┴──────────────────────────┘

1. Standard K-Fold Cross-Validation

Splits the dataset into $K$ equal non-overlapping chunks (folds, typically $K=5$ or $K=10$).

  Fold 1: [ VALID ] [ TRAIN ] [ TRAIN ] [ TRAIN ] [ TRAIN ] ──► Score 1
  Fold 2: [ TRAIN ] [ VALID ] [ TRAIN ] [ TRAIN ] [ TRAIN ] ──► Score 2
  ...
  Final Score = Mean( Score 1, Score 2, Score 3, Score 4, Score 5 )

Train $K$ independent models. The final performance score is the average across all $K$ validation folds.

2. Stratified K-Fold (For Imbalanced Classification)

If your dataset contains 95% Non-Fraud and 5% Fraud transactions:

Standard randomized K-Fold might create a validation fold containing 0% Fraud samples by random chance!

Stratified K-Fold guarantees that every single fold contains the exact same 95% to 5% class ratio as the original dataset.

3. Group K-Fold (Preventing Patient / User Data Leakage)

Suppose your dataset contains 1000 medical X-rays taken from 50 individual patients (20 images per patient).

If you use standard K-Fold, images from Patient #12 will land in both the training fold AND the validation fold!

The model will memorize Patient #12's unique bone shape, giving a fake 99% validation score (Data Leakage).

Group K-Fold ensures that all 20 images from Patient #12 stay in the same fold. A patient's data is either 100% in training or 100% in validation.

4. Time Series Split (Preventing Temporal Leakage)

In stock pricing or weather forecasting, future events cannot predict past events.

  Split 1: [ Train: Month 1 ] ──► [ Valid: Month 2 ]
  Split 2: [ Train: Month 1, 2 ] ──► [ Valid: Month 3 ]
  Split 3: [ Train: Month 1, 2, 3 ] ──► [ Valid: Month 4 ]

Time Series Split uses an expanding window where training data always precedes validation data in time.

Say this out loud

Cross-validation evaluates model generalization across multiple data splits. Standard K-Fold averages scores across K equal folds. Stratified K-Fold preserves class label ratios for imbalanced data. Group K-Fold keeps entire subjects or users in the same fold to prevent data leakage. Time Series Split uses expanding past to future windows to respect temporal causality.

Followups to expect

  1. What is Leave One Out Cross Validation (LOOCV)? Setting $K = N$ (where $N$ is total sample count). Trains $N$ models where each model validates on a single sample. Computationally expensive, but useful for tiny datasets ($N < 50$).
  2. Why must feature scaling (StandardScaler) be fitted INSIDE each CV loop? If you apply StandardScaler.fit(X) to the full dataset before cross validation, the scaler calculates global mean and variance from validation samples, causing Data Leakage. Always fit scalers on training folds only.

Check yourself

Question 1 of 3

Why must Stratified K-Fold be selected over standard K-Fold for an imbalanced dataset with 99 percent Class 0 and 1 percent Class 1?

More in Classical ML

See all →
Bias–Variance Tradeoff4 minOverfitting vs Underfitting3 minLinear Regression4 min