Cross-Validation
116 words
1 min read
Visual companion
Python
Type and operator map
Python Week 1: the first filter for runtime behavior
View
Revision summary
What this note is really saying
Short form
# Cross-Validation ## 3.1 K-Fold Cross-Validation Split data into $k$ folds. Train on $k-1$ folds, test on the remaining fold.

Cross-Validation
3.1 K-Fold Cross-Validation
Split data into k folds. Train on k−1 folds, test on the remaining fold. Repeat k times.
pythonfrom sklearn.model_selection import cross_val_score from sklearn.linear_model import Ridge import numpy as np X = np.random.randn(100, 5) y = X[:, 0] + 0.5 * X[:, 1] + np.random.randn(100) * 0.1 model = Ridge(alpha=1.0) scores = cross_val_score(model, X, y, cv=5, scoring='r2') print(f"R2 scores: {scores}, mean: {scores.mean():.3f}")
3.2 LOOCV (Leave-One-Out)
k=n: train on n−1 observations, test on the one left out. High variance, computationally expensive.
Bias-Variance Tradeoff
- k=2: low variance, high bias
- k=n (LOOCV): low bias, high variance
- k=5 or k=10: good compromise Join Discord PreviousBootstrapNextGradient Descent