Cross-Validation
🎯 Learning Objectives
- Understand hold-out validation and its limitations
- Apply K-Fold and Stratified K-Fold Cross-Validation
- Know when to use LOOCV, Repeated CV, and Bootstrap
- Compute cross-validation metrics for GATE numerical problems
1. Hold-Out Validation
The simplest approach: split the dataset into a training set and a test set (e.g., 70/30 or 80/20). Train on the training set, evaluate on the test set.
For hyperparameter tuning, a three-way split is used:
- Training set (60%): Fit model parameters
- Validation set (20%): Tune hyperparameters, select model
- Test set (20%): Final unbiased performance estimate
- Performance estimate depends on which data ends up in which split (high variance)
- Wastes data — not all data is used for training
- Unreliable for small datasets
- A “lucky” or “unlucky” split can give misleading results
2. K-Fold Cross-Validation
K-Fold CV provides a more reliable performance estimate by training and evaluating K times, each time using a different fold as the validation set.
Input: Dataset D with n samples, number of folds K 1. SHUFFLE: Randomly shuffle the dataset 2. SPLIT: Divide data into K roughly equal-sized folds: F_1, F_2, ..., F_K 3. FOR i = 1 to K: a. Use F_i as the validation set b. Use the remaining K-1 folds as the training set c. Train the model on the training set d. Evaluate on F_i, record score_i 4. RESULT: CV score = (1/K) ∑ score_i Optionally compute standard deviation for confidence
Q: A dataset has 100 samples. You perform 5-Fold CV. In each fold, you train on ___ samples and validate on ___ samples. How many total models are trained?
A: Each fold: train on 80 samples, validate on 20 samples. Total models trained: 5. Each sample appears exactly once as a validation sample and K−1 = 4 times as a training sample.
Typical K values:
- K = 5: Good balance between bias and computational cost. Most common in practice.
- K = 10: Slightly less biased estimate. Standard recommendation.
- K = n (LOOCV): Least biased but highest variance and computation.
2.1 Stratified K-Fold
In stratified K-Fold, each fold preserves the class distribution of the original dataset. Essential for imbalanced datasets.
Dataset: 90% Class A, 10% Class B (100 samples total). Regular 5-fold might create a fold with 0 Class B samples. Stratified 5-fold ensures each fold has 18 Class A and 2 Class B samples, maintaining the 90/10 ratio.
3. Leave-One-Out Cross-Validation (LOOCV)
LOOCV is K-Fold CV with K = n (number of samples). Each iteration uses exactly one sample as the validation set and the remaining n−1 as training.
- Nearly unbiased: Each model is trained on n−1 samples (almost all data)
- High variance: The n training sets overlap heavily (differ by only 1 sample), so the n models are highly correlated, and their average has high variance
- Computational cost: Requires training n separate models. Expensive for large datasets.
- Deterministic: No randomness in the split (unlike K-Fold with shuffling)
Q: Using LOOCV on a dataset of 5 points with a 1-NN classifier: Points (1,+), (2,−), (3,+), (4,−), (5,+). Compute LOOCV accuracy.
Solution: For 1-NN, the nearest neighbour of the left-out point determines the prediction:
- Leave out (1,+): nearest is (2,−) → predict −, actual + → Wrong
- Leave out (2,−): nearest is (1,+) or (3,+) (both distance 1) → predict + → actual − → Wrong
- Leave out (3,+): nearest is (2,−) or (4,−) → predict −, actual + → Wrong
- Leave out (4,−): nearest is (3,+) or (5,+) → predict +, actual − → Wrong
- Leave out (5,+): nearest is (4,−) → predict −, actual + → Wrong
LOOCV accuracy = 0/5 = 0% (0.0). This alternating pattern is the worst case for 1-NN with LOOCV!
4. Repeated Cross-Validation
Perform K-Fold CV multiple times with different random shuffles, then average all results. This reduces the variance of the CV estimate.
Repeat K-Fold CV r times with different random splits. Total models trained: r × K. Final score is the average of all r × K evaluations.
Common choice: 10-fold CV repeated 3 times (30 total evaluations).
5. Bootstrap Method
The bootstrap creates training sets by sampling with replacement from the original dataset. Some samples appear multiple times, others not at all.
For b = 1 to B:
1. Draw n samples WITH REPLACEMENT from D (some will be duplicates)
2. Train model on the bootstrap sample
3. Evaluate on the OUT-OF-BAG (OOB) samples:
those not selected in this bootstrap
4. Record the OOB error
Final estimate: Average of B OOB errors
For a dataset of size n, the probability that a specific sample is NOT selected in any of the n draws is:
P(OOB) = (1 − 1/n)ⁿ ≈ e⁻¹ ≈ 0.368
So about 36.8% of the data ends up as OOB (test set) in each bootstrap sample. The training set uses about 63.2% unique samples.
Error₆₃₂ = 0.368 × (training error) + 0.632 × (OOB error)
This corrects for the upward bias of the OOB error estimate (because training sets are smaller than the full dataset).
6. When to Use Which Method
| Method | Dataset Size | Bias | Variance | Computation |
|---|---|---|---|---|
| Hold-out | Large (>10K) | High (wastes data) | High | Low (1 model) |
| 5-Fold CV | Medium | Moderate | Moderate | Moderate (5 models) |
| 10-Fold CV | Medium | Lower | Moderate | Moderate (10 models) |
| LOOCV | Small (<100) | Very low | High | High (n models) |
| Repeated CV | Medium-Small | Low | Low | High (r×K models) |
| Bootstrap | Small | Low (.632) | Low | High (B models) |
7. GATE-Style Practice
Q: In 10-fold CV with 1000 samples, how many samples are used for training in each fold?
A: Each fold has 1000/10 = 100 samples for validation, 900 for training. Answer: 900.
Q: 5-Fold CV yields accuracies: 85%, 88%, 82%, 90%, 85%. What is the CV score?
A: CV score = (85+88+82+90+85)/5 = 430/5 = 86%. Standard deviation = √(((85-86)²+(88-86)²+(82-86)²+(90-86)²+(85-86)²)/5) = √(1+4+16+16+1)/5 = √7.6 ≈ 2.76%.
Q: In bootstrap sampling of a dataset with 50 samples, approximately how many unique samples appear in each bootstrap sample?
A: Approximately 50 × (1 − e⁻¹) ≈ 50 × 0.632 ≈ 31.6 ≈ 32 unique samples. The remaining ~18 are out-of-bag.