Machine Learning1 code example
⚡ +100 XP

Cross-Validation

1

Why Cross-Validation?

A single train/test split gives a high-variance estimate of generalization performance — it depends heavily on which samples ended up in each split. Cross-validation averages over K different splits for a more reliable estimate.

2

K-Fold Cross-Validation

1. Split data into K equal folds 2. For each fold i: train on all folds except i, evaluate on fold i 3. Average the K evaluation scores K=5 or K=10 is standard. K=N (Leave-One-Out) is most thorough but slow.

3

Stratified K-Fold

Preserves the class distribution in each fold. Always use this for classification tasks — especially with imbalanced data.

4

Time Series Split

For time-series data, you must never let future data leak into training. TimeSeriesSplit always trains on past data and validates on future data.

5

Cross-Validation Strategies

💡

Use cross_validate (not cross_val_score) when you need multiple metrics — avoids fitting the model multiple times.

Finished reading? Mark it complete to earn your XP.