Home / Machine Learning

Cross Validation

Interactive explorer to visualize K-Fold splitting and validation logic.

Configuration

5

Higher k creates more folds but requires more training iterations.


Visualization

Fold: - / -
Train Data
Test Fold

Results

Ready to start.
Click 'Run Next Fold'
Average Accuracy
--%

Understanding Cross-Validation

How can you be confident that your machine learning model will perform well on new, unseen data? A simple train/test split is a good start, but the result can be lucky (or unlucky) depending on which data points happen to land in the test set. Cross-Validation (CV) is a more robust and reliable technique to estimate model performance by using every part of the dataset for both training and testing.

The Problem with a Single Split

When you split your data into just one training set and one testing set, you are making a bet. If the test set happens to contain particularly "easy" or "hard" examples by random chance, your evaluation score might be overly optimistic or pessimistic. Your model's score becomes dependent on the specific random split you chose.

Enter K-Fold Cross-Validation

K-Fold Cross-Validation solves this problem by performing multiple train/test splits and averaging the results. The process, which you can simulate in the lab above, is as follows:

  1. Shuffle & Split: The entire dataset is first shuffled randomly. Then, it is split into 'k' equal-sized chunks, or "folds". You can control 'k' with the slider.
  2. Iterate and Train: The process then iterates 'k' times. In each iteration (or "fold run"):
    • One fold is held out as the test set (highlighted in orange).
    • The remaining k-1 folds are combined to form the training set (highlighted in blue).
    • The model is trained on the training set and evaluated on the test set, producing a score for that fold.
  3. Average the Scores: After running through all 'k' folds, you will have 'k' different performance scores. The final cross-validation score is the average of these individual scores. This average provides a much more stable and reliable estimate of how your model will perform on unseen data.

Guided Experiments

Use the interactive tool to solidify your understanding.

  1. Walk Through a 5-Fold CV: Set the Folds (k) to 5. Click the "Run Next Fold" button. Watch as the first fold (the first 20% of the data) becomes the test set and the rest becomes the training set. A score for Fold 1 appears in the results. Click it again. Now, the second fold becomes the test set, and all others are used for training. Repeat this five times until every data point has been in a test set exactly once.
  2. Change the Number of Folds (k): Reset the simulation and set k=3. Run through the folds. Notice how the test set is now much larger (1/3 of the data). Now, set k=10. The test set is smaller (1/10 of the data), but you have to run more iterations. A common choice for 'k' is 5 or 10, as it provides a good balance between computational cost and a reliable estimate.
  3. The Importance of Shuffling: Imagine if your data was sorted (e.g., all "Class A" first, then all "Class B"). If you didn't shuffle, your first few folds might only contain "Class A"! The "Shuffle" button randomizes the data order, which is a crucial first step to ensure that each fold is a representative sample of the overall dataset.

Key Takeaways

  • More Reliable Than a Single Split: Cross-validation gives a more robust estimate of model performance by reducing the variance associated with a single, random train/test split.
  • Uses All Your Data: Every single data point gets to be in a test set once, meaning you are using your precious data more efficiently.
  • The Standard for Model Evaluation: K-Fold CV is the gold standard for comparing different models or tuning hyperparameters. If Model A has a better average CV score than Model B, you can be more confident that it's truly the better model.
  • Choosing 'k': A higher 'k' means less bias in the performance estimate but is more computationally expensive. A lower 'k' is faster but might have more bias. k=5 or k=10 are common, practical choices.
Cheat sheet

K-Fold Cross Validation

How can you be confident that your machine learning model will perform well on new, unseen data? A simple train/test split is a good start, but the result can be lucky (or unlucky) depending on which data points happen to land in the test set. Cross-Validation (CV) is a more robust and reliable technique to estimate model performance by using every part of the dataset for both training and testing.

MACHINE LEARNING · vizlearn.in/machine_learning/cross_validation.html