Cross-Validation: Reliable Model Evaluation Techniques
Learn how to evaluate ML models reliably using k-fold, stratified, leave-one-out, and time series cross-validation to avoid overfitting and get honest performance estimates.
Cross-Validation: Reliable Model Evaluation Techniques
A model that performs well on training data but poorly on new data is overfitting. Cross-validation is the standard technique to get an honest estimate of how your model will perform on unseen data.
---
Why You Need Cross-Validation
The Problem with a Simple Train/Test Split
A single train/test split (e.g., 80/20) has a weakness: the result depends on which rows ended up in the test set. You might get lucky or unlucky.
| Approach | Reliability | Data Efficiency |
|---|---|---|
| Single train/test split | Low — one estimate only | Wastes 20% of data |
| K-Fold cross-validation | High — K estimates averaged | Uses all data for both training and testing |
K-Fold Cross-Validation
The most common method. It splits your data into K equal parts (folds), then trains and tests K times.
How It Works
- Split the dataset into K folds (e.g., K=5)
- Round 1: Train on folds 2-5, test on fold 1
- Round 2: Train on folds 1,3-5, test on fold 2
- Round 3: Train on folds 1-2,4-5, test on fold 3
- Continue until every fold has been the test set once
- Average the K test scores
Choosing K
| K Value | Pros | Cons | When to Use |
|---|---|---|---|
| K=5 | Good balance of bias and variance | Moderate compute | Default choice, most datasets |
| K=10 | Lower bias, more stable estimate | More compute time | Medium datasets (1K-50K rows) |
| K=3 | Fast | Higher variance in estimates | Large datasets (100K+ rows) |
| K=20+ | Very low bias | High variance, slow | Special cases |
---
Stratified K-Fold
For classification tasks, regular k-fold might create folds where one class is underrepresented. Stratified k-fold ensures each fold has approximately the same proportion of each class.
When to Use
- Classification problems
- Imbalanced datasets (e.g., 95% "No", 5% "Yes")
- Any time target classes are unevenly distributed
Example
Dataset: 100 rows, 90 negative, 10 positive
| Method | Fold composition | Risk |
|---|---|---|
| Regular K-Fold | A fold might have 0 positive samples | Unreliable accuracy estimate |
| Stratified K-Fold | Each fold has ~9 negative, ~1 positive | Fair evaluation |
Leave-One-Out Cross-Validation (LOOCV)
An extreme case where K = N (number of samples). Each round trains on N-1 samples and tests on 1.
| Pros | Cons |
|---|---|
| Uses maximum data for training | Extremely slow for large datasets |
| No randomness in split | High variance in each individual estimate |
| Deterministic result | Not practical for N > 10,000 |
---
Time Series Cross-Validation
Standard k-fold randomly shuffles data, which breaks temporal order. For time series, you must respect the time sequence.
Walk-Forward Validation
- Train on data up to time T, test on T+1 to T+h
- Expand training to include T+1, test on T+2 to T+h+1
- Continue expanding the training window
Sliding Window Validation
Same as walk-forward, but the training window stays a fixed size (older data is dropped).
| Method | Training Window | Best For |
|---|---|---|
| Expanding Window | Grows over time | Stable patterns, enough data |
| Sliding Window | Fixed size | Changing patterns, concept drift |
---
Repeated Cross-Validation
Run K-fold multiple times with different random splits and average all results. Reduces variance in the estimate.
Example: 5-fold CV repeated 3 times = 15 total evaluations.
Use when: You need a very stable estimate and have the compute budget.
---
Nested Cross-Validation
Used when you are doing both hyperparameter tuning and model evaluation. It prevents the evaluation from being biased by the tuning process.
Structure
- Outer loop (e.g., 5-fold): Evaluates final model performance
- Inner loop (e.g., 3-fold): Tunes hyperparameters
Why It Matters
Without nested CV, you tune and evaluate on the same data, which leaks information and gives overly optimistic results.
---
Cross-Validation in Practice
How to Use in the Platform
- Go to ML → Train
- Select your dataset and target column
- In the configuration panel, find Validation Strategy
- Choose: Train/Test Split, K-Fold, or Stratified K-Fold
- Set K (default is 5)
- Train — the platform reports the average CV score and per-fold scores
Interpreting Results
| Metric | What to Look For |
|---|---|
| Average CV score | Your best estimate of real-world performance |
| Per-fold scores | If they vary wildly, your model may be unstable |
| Standard deviation across folds | Low SD = stable model; high SD = investigate |
| Train vs CV score gap | Large gap = overfitting |
Common Pitfalls
| Pitfall | Problem | Fix |
|---|---|---|
| Preprocessing before splitting | Data leakage — test set info leaks into training | Always split first, then preprocess each fold independently |
| Shuffling time series data | Future data in training set | Use time series CV |
| Using train accuracy as final metric | Overly optimistic | Always report CV or test set accuracy |
| Not stratifying for imbalanced data | Folds have different class distributions | Use stratified k-fold |
| Tuning + evaluating on same CV | Biased estimate | Use nested cross-validation |
Quick Reference
| Method | K | Best For | Preserves Time Order? |
|---|---|---|---|
| K-Fold | 5-10 | General purpose | No |
| Stratified K-Fold | 5-10 | Classification, imbalanced | No |
| LOOCV | N | Very small datasets | No |
| Time Series CV | Varies | Temporal data | Yes |
| Repeated K-Fold | 5×3 | Stable estimates | No |
| Nested CV | 5×3 | Tuning + evaluation | No |
What's Next?
- [Model Evaluation Metrics](model-evaluation-metrics) — Understand accuracy, precision, recall, F1, AUC, RMSE
- [Hyperparameter Tuning](hyperparameter-tuning) — Use CV to find the best model settings
- [Bias-Variance Tradeoff](bias-variance-overfitting) — Why models overfit and how to prevent it
- [ML Overview](ml-overview) — Full ML workflow
Related Articles
Machine Learning Overview: Workflow and Getting Started
A beginner-friendly recap of ML, classification vs regression vs clustering, the ML workflow (select, configure, train, evaluate, predict), and a 5-minute quick-start guide.
Model Evaluation Metrics: Accuracy, Precision, Recall, F1, AUC, RMSE, and More
Understand all major ML evaluation metrics for classification and regression, learn when to use each one, and avoid common metric selection mistakes.
Hyperparameter Tuning: Grid Search, Random Search, and Bayesian Optimization
Learn how to find the best settings for your ML models using grid search, random search, and Bayesian optimization, with practical guidance on what to tune for each algorithm.
Bias-Variance Tradeoff: Understanding Overfitting and Underfitting
Master the bias-variance tradeoff, understand what causes overfitting and underfitting, learn how to detect them with learning curves, and apply regularization (L1/L2), early stopping, and cross-validation to build better models.
Was this helpful?