Data Science Foundations · Intermediate

Cross-Validation Visualizer

Watch k-fold validation rotate: split → refit on k−1 folds → predict the held-out fold → score → repeat → aggregate.

A genuine k-fold teaching experiment using a tiny nearest-centroid classifier. Every fold recomputes class centroids from its training subset, validates only on the held-out fold, and contributes one real fold score to the final mean and spread.

Step by step

  1. Keep a final-test subset outside the cross-validation pool.
  2. Partition the CV pool into k non-overlapping validation folds, optionally stratified by class.
  3. For the active fold, fit the two class centroids using only the other k−1 folds.
  4. Predict each held-out point by the nearest fitted centroid and calculate validation accuracy.
  5. Rotate the validation role and refit from scratch for every fold.
  6. Aggregate the k held-out scores into a mean and fold-score standard deviation.

Core formulas

Nearest centroid

ŷ = argminₖ ||x − μₖ||²

The tiny teaching classifier predicts the class whose training centroid is closest.

Fold accuracy

Accuracyⱼ = correctⱼ / nⱼ

Each fold is scored only on samples excluded from that fold's fit.

CV mean

CV = (1/k) Σ Accuracyⱼ

K-fold cross-validation summarizes validation performance across the rotations.

When to use Cross-Validation Visualizer

  • Understanding why validation data must not fit the model it evaluates.
  • Learning k-fold rotation and why every fold requires a genuine refit.
  • Comparing ordinary and stratified class allocation while keeping final-test evidence separate.

Primary references

scikit-learn — Cross-validation: evaluating estimator performance

scikit-learn — KFold