Cross Validation

Model Selection and Performance Estimation

$$CV_k = \frac{1}{k} \sum_{i=1}^k L(f^{(-i)}, D_i)$$
Core Problem: How do we reliably estimate model performance with limited data?
Key Insight: Cross validation provides a more robust and less biased estimate of model performance than a single train-test split

The Data Splitting Problem

Why Simple Train-Test Splits Are Insufficient

Problems with Simple Train-Test Split:
Example Scenario: 1000 samples, 80-20 split
Cross Validation Solution: Use all data for both training and validation through systematic resampling

K-Fold Cross Validation

The Standard Approach

$$CV_k = \frac{1}{k} \sum_{i=1}^k L(f^{(-i)}, D_i)$$ $$\text{where } f^{(-i)} \text{ is trained on all folds except } i$$
K-Fold Procedure:
  1. Split: Divide dataset into k equal-sized folds
  2. Iterate: For each fold i = 1, ..., k:
    • Use fold i as validation set
    • Use remaining k-1 folds as training set
    • Train model and evaluate on fold i
  3. Average: Compute mean performance across all folds
Common Choices:

Cross Validation Variants

Adapting to Different Data Types

Method Use Case Pros Cons
Standard k-Fold General purpose Balanced bias-variance Random splits may not preserve structure
Stratified k-Fold Classification with imbalanced classes Preserves class distribution Only for classification
Leave-One-Out (LOOCV) Very small datasets Maximum use of data High variance, expensive
Time Series CV Temporal data Respects temporal order Less data for validation
Group k-Fold Grouped/clustered data Avoids data leakage Uneven fold sizes
Time Series Example: Predict stock prices

Nested Cross Validation

Unbiased Model Selection

Problem: Using cross validation for both hyperparameter tuning AND performance estimation introduces optimistic bias
Solution: Two-level cross validation
Nested CV Structure:
For each outer fold i = 1, ..., k₁: For each hyperparameter configuration h: Inner CV score = k₂-fold CV on training data Select best h* based on inner CV Train model with h* on full training data Evaluate on outer test fold i Return: Average performance across outer folds
$$\text{Unbiased Estimate} = \frac{1}{k_1} \sum_{i=1}^{k_1} L(f^*_{(-i)}, D_i^{test})$$
Cost: k₁ × k₂ × |hyperparameters| model trainings

Evaluation Metrics in Cross Validation

Choosing the Right Metric

Classification

  • Accuracy: Overall correctness
  • Precision: True positive rate
  • Recall: Sensitivity
  • F1-Score: Harmonic mean
  • AUC-ROC: Ranking quality

Regression

  • MAE: Mean Absolute Error
  • MSE: Mean Squared Error
  • RMSE: Root MSE
  • R²: Coefficient of determination
  • MAPE: Mean Absolute Percentage Error
Metric Selection Guidelines:
$$F1 = 2 \cdot \frac{\text{Precision} \cdot \text{Recall}}{\text{Precision} + \text{Recall}}$$

Statistical Considerations

Confidence and Significance

Key Statistical Concepts:
$$\text{CI} = \bar{s} \pm t_{\alpha/2, k-1} \cdot \frac{\sigma_s}{\sqrt{k}}$$ $$\text{where } \bar{s} = \frac{1}{k}\sum_{i=1}^k s_i$$
Paired t-test for Model Comparison:
Rule of Thumb: CV score ± 2×standard_error gives ~95% confidence interval

Common Pitfalls and How to Avoid Them

Cross Validation Mistakes

❌ Data Leakage in Preprocessing
✅ Correct Approach: Fit preprocessing only on training folds
❌ Ignoring Temporal Dependencies
✅ Correct Approach: Use time-aware validation strategies
❌ Inappropriate for Imbalanced Data
✅ Correct Approach: Use stratified k-fold to preserve class distribution

Implementation Best Practices

Practical Cross Validation

from sklearn.model_selection import cross_val_score, StratifiedKFold from sklearn.pipeline import Pipeline from sklearn.preprocessing import StandardScaler from sklearn.ensemble import RandomForestClassifier # Correct: Preprocessing inside pipeline pipeline = Pipeline([ ('scaler', StandardScaler()), ('classifier', RandomForestClassifier()) ]) # Stratified k-fold for classification cv = StratifiedKFold(n_splits=5, shuffle=True, random_state=42) scores = cross_val_score(pipeline, X, y, cv=cv, scoring='f1_macro') print(f"CV Score: {scores.mean():.3f} ± {scores.std():.3f}")
Key Implementation Points:
Pro Tip: Always use the same CV folds when comparing different models

Key Takeaways

Cross Validation Essentials

Golden Rule: Cross validation is essential for reliable machine learning, but must be implemented correctly to avoid subtle biases
$$\text{Good ML Practice} = \text{Proper CV} + \text{Appropriate Metrics} + \text{Statistical Rigor}$$
Remember: The goal is not just to get a number, but to get a trustworthy estimate of how your model will perform on new, unseen data
Slide 1 of 10