Interactive validation playground

Cross-validation split visualizer

Compare six cross-validation splitters in JavaScript. Inspect train and test indices, class balance, entity overlap, and time leakage in every fold.

Free to useNo upload or sign-inRuns entirely in your browser
Live CV split audit
Powered by @kanaries/ml

KFold shares entities across train and test in 40 fold/group pairs. For predictions on new entities, try StratifiedGroupKFold. For forecasting, 93 training rows across folds occur after validation begins.

Cross-validation split diagram

KFoldSample 0Sample 47Class label (y)Sample 0 · y=0 · G10Sample 1 · y=0 · G20Sample 2 · y=1 · G31Sample 3 · y=0 · G40Sample 4 · y=1 · G51Sample 5 · y=0 · G60Sample 6 · y=1 · G71Sample 7 · y=0 · G80Sample 8 · y=0 · G10Sample 9 · y=0 · G20Sample 10 · y=1 · G31Sample 11 · y=0 · G40Sample 12 · y=0 · G50Sample 13 · y=0 · G60Sample 14 · y=1 · G71Sample 15 · y=0 · G80Sample 16 · y=1 · G11Sample 17 · y=0 · G20Sample 18 · y=1 · G31Sample 19 · y=0 · G40Sample 20 · y=0 · G50Sample 21 · y=0 · G60Sample 22 · y=0 · G70Sample 23 · y=0 · G80Sample 24 · y=0 · G10Sample 25 · y=0 · G20Sample 26 · y=1 · G31Sample 27 · y=0 · G40Sample 28 · y=1 · G51Sample 29 · y=0 · G60Sample 30 · y=1 · G71Sample 31 · y=0 · G80Sample 32 · y=1 · G11Sample 33 · y=0 · G20Sample 34 · y=1 · G31Sample 35 · y=0 · G40Sample 36 · y=0 · G50Sample 37 · y=0 · G60Sample 38 · y=1 · G71Sample 39 · y=0 · G80Sample 40 · y=0 · G10Sample 41 · y=0 · G20Sample 42 · y=1 · G31Sample 43 · y=0 · G40Sample 44 · y=0 · G50Sample 45 · y=0 · G60Sample 46 · y=0 · G70Sample 47 · y=0 · G80Entity groupSample 0 · y=0 · G11Sample 1 · y=0 · G22Sample 2 · y=1 · G33Sample 3 · y=0 · G44Sample 4 · y=1 · G55Sample 5 · y=0 · G66Sample 6 · y=1 · G77Sample 7 · y=0 · G88Sample 8 · y=0 · G11Sample 9 · y=0 · G22Sample 10 · y=1 · G33Sample 11 · y=0 · G44Sample 12 · y=0 · G55Sample 13 · y=0 · G66Sample 14 · y=1 · G77Sample 15 · y=0 · G88Sample 16 · y=1 · G11Sample 17 · y=0 · G22Sample 18 · y=1 · G33Sample 19 · y=0 · G44Sample 20 · y=0 · G55Sample 21 · y=0 · G66Sample 22 · y=0 · G77Sample 23 · y=0 · G88Sample 24 · y=0 · G11Sample 25 · y=0 · G22Sample 26 · y=1 · G33Sample 27 · y=0 · G44Sample 28 · y=1 · G55Sample 29 · y=0 · G66Sample 30 · y=1 · G77Sample 31 · y=0 · G88Sample 32 · y=1 · G11Sample 33 · y=0 · G22Sample 34 · y=1 · G33Sample 35 · y=0 · G44Sample 36 · y=0 · G55Sample 37 · y=0 · G66Sample 38 · y=1 · G77Sample 39 · y=0 · G88Sample 40 · y=0 · G11Sample 41 · y=0 · G22Sample 42 · y=1 · G33Sample 43 · y=0 · G44Sample 44 · y=0 · G55Sample 45 · y=0 · G66Sample 46 · y=0 · G77Sample 47 · y=0 · G88Fold 1Sample 0 · y=0 · G1 · Test · shared groupSample 1 · y=0 · G2 · Test · shared groupSample 2 · y=1 · G3 · Test · shared groupSample 3 · y=0 · G4 · Test · shared groupSample 4 · y=1 · G5 · Test · shared groupSample 5 · y=0 · G6 · Test · shared groupSample 6 · y=1 · G7 · Test · shared groupSample 7 · y=0 · G8 · Test · shared groupSample 8 · y=0 · G1 · Test · shared groupSample 9 · y=0 · G2 · Test · shared groupSample 10 · y=1 · G3 · Train · shared groupSample 11 · y=0 · G4 · Train · shared groupSample 12 · y=0 · G5 · Train · shared groupSample 13 · y=0 · G6 · Train · shared groupSample 14 · y=1 · G7 · Train · shared groupSample 15 · y=0 · G8 · Train · shared groupSample 16 · y=1 · G1 · Train · shared groupSample 17 · y=0 · G2 · Train · shared groupSample 18 · y=1 · G3 · Train · shared groupSample 19 · y=0 · G4 · Train · shared groupSample 20 · y=0 · G5 · Train · shared groupSample 21 · y=0 · G6 · Train · shared groupSample 22 · y=0 · G7 · Train · shared groupSample 23 · y=0 · G8 · Train · shared groupSample 24 · y=0 · G1 · Train · shared groupSample 25 · y=0 · G2 · Train · shared groupSample 26 · y=1 · G3 · Train · shared groupSample 27 · y=0 · G4 · Train · shared groupSample 28 · y=1 · G5 · Train · shared groupSample 29 · y=0 · G6 · Train · shared groupSample 30 · y=1 · G7 · Train · shared groupSample 31 · y=0 · G8 · Train · shared groupSample 32 · y=1 · G1 · Train · shared groupSample 33 · y=0 · G2 · Train · shared groupSample 34 · y=1 · G3 · Train · shared groupSample 35 · y=0 · G4 · Train · shared groupSample 36 · y=0 · G5 · Train · shared groupSample 37 · y=0 · G6 · Train · shared groupSample 38 · y=1 · G7 · Train · shared groupSample 39 · y=0 · G8 · Train · shared groupSample 40 · y=0 · G1 · Train · shared groupSample 41 · y=0 · G2 · Train · shared groupSample 42 · y=1 · G3 · Train · shared groupSample 43 · y=0 · G4 · Train · shared groupSample 44 · y=0 · G5 · Train · shared groupSample 45 · y=0 · G6 · Train · shared groupSample 46 · y=0 · G7 · Train · shared groupSample 47 · y=0 · G8 · Train · shared groupFold 2Sample 0 · y=0 · G1 · Train · shared groupSample 1 · y=0 · G2 · Train · shared groupSample 2 · y=1 · G3 · Train · shared groupSample 3 · y=0 · G4 · Train · shared groupSample 4 · y=1 · G5 · Train · shared groupSample 5 · y=0 · G6 · Train · shared groupSample 6 · y=1 · G7 · Train · shared groupSample 7 · y=0 · G8 · Train · shared groupSample 8 · y=0 · G1 · Train · shared groupSample 9 · y=0 · G2 · Train · shared groupSample 10 · y=1 · G3 · Test · shared groupSample 11 · y=0 · G4 · Test · shared groupSample 12 · y=0 · G5 · Test · shared groupSample 13 · y=0 · G6 · Test · shared groupSample 14 · y=1 · G7 · Test · shared groupSample 15 · y=0 · G8 · Test · shared groupSample 16 · y=1 · G1 · Test · shared groupSample 17 · y=0 · G2 · Test · shared groupSample 18 · y=1 · G3 · Test · shared groupSample 19 · y=0 · G4 · Test · shared groupSample 20 · y=0 · G5 · Train · shared groupSample 21 · y=0 · G6 · Train · shared groupSample 22 · y=0 · G7 · Train · shared groupSample 23 · y=0 · G8 · Train · shared groupSample 24 · y=0 · G1 · Train · shared groupSample 25 · y=0 · G2 · Train · shared groupSample 26 · y=1 · G3 · Train · shared groupSample 27 · y=0 · G4 · Train · shared groupSample 28 · y=1 · G5 · Train · shared groupSample 29 · y=0 · G6 · Train · shared groupSample 30 · y=1 · G7 · Train · shared groupSample 31 · y=0 · G8 · Train · shared groupSample 32 · y=1 · G1 · Train · shared groupSample 33 · y=0 · G2 · Train · shared groupSample 34 · y=1 · G3 · Train · shared groupSample 35 · y=0 · G4 · Train · shared groupSample 36 · y=0 · G5 · Train · shared groupSample 37 · y=0 · G6 · Train · shared groupSample 38 · y=1 · G7 · Train · shared groupSample 39 · y=0 · G8 · Train · shared groupSample 40 · y=0 · G1 · Train · shared groupSample 41 · y=0 · G2 · Train · shared groupSample 42 · y=1 · G3 · Train · shared groupSample 43 · y=0 · G4 · Train · shared groupSample 44 · y=0 · G5 · Train · shared groupSample 45 · y=0 · G6 · Train · shared groupSample 46 · y=0 · G7 · Train · shared groupSample 47 · y=0 · G8 · Train · shared groupFold 3Sample 0 · y=0 · G1 · Train · shared groupSample 1 · y=0 · G2 · Train · shared groupSample 2 · y=1 · G3 · Train · shared groupSample 3 · y=0 · G4 · Train · shared groupSample 4 · y=1 · G5 · Train · shared groupSample 5 · y=0 · G6 · Train · shared groupSample 6 · y=1 · G7 · Train · shared groupSample 7 · y=0 · G8 · Train · shared groupSample 8 · y=0 · G1 · Train · shared groupSample 9 · y=0 · G2 · Train · shared groupSample 10 · y=1 · G3 · Train · shared groupSample 11 · y=0 · G4 · Train · shared groupSample 12 · y=0 · G5 · Train · shared groupSample 13 · y=0 · G6 · Train · shared groupSample 14 · y=1 · G7 · Train · shared groupSample 15 · y=0 · G8 · Train · shared groupSample 16 · y=1 · G1 · Train · shared groupSample 17 · y=0 · G2 · Train · shared groupSample 18 · y=1 · G3 · Train · shared groupSample 19 · y=0 · G4 · Train · shared groupSample 20 · y=0 · G5 · Test · shared groupSample 21 · y=0 · G6 · Test · shared groupSample 22 · y=0 · G7 · Test · shared groupSample 23 · y=0 · G8 · Test · shared groupSample 24 · y=0 · G1 · Test · shared groupSample 25 · y=0 · G2 · Test · shared groupSample 26 · y=1 · G3 · Test · shared groupSample 27 · y=0 · G4 · Test · shared groupSample 28 · y=1 · G5 · Test · shared groupSample 29 · y=0 · G6 · Test · shared groupSample 30 · y=1 · G7 · Train · shared groupSample 31 · y=0 · G8 · Train · shared groupSample 32 · y=1 · G1 · Train · shared groupSample 33 · y=0 · G2 · Train · shared groupSample 34 · y=1 · G3 · Train · shared groupSample 35 · y=0 · G4 · Train · shared groupSample 36 · y=0 · G5 · Train · shared groupSample 37 · y=0 · G6 · Train · shared groupSample 38 · y=1 · G7 · Train · shared groupSample 39 · y=0 · G8 · Train · shared groupSample 40 · y=0 · G1 · Train · shared groupSample 41 · y=0 · G2 · Train · shared groupSample 42 · y=1 · G3 · Train · shared groupSample 43 · y=0 · G4 · Train · shared groupSample 44 · y=0 · G5 · Train · shared groupSample 45 · y=0 · G6 · Train · shared groupSample 46 · y=0 · G7 · Train · shared groupSample 47 · y=0 · G8 · Train · shared groupFold 4Sample 0 · y=0 · G1 · Train · shared groupSample 1 · y=0 · G2 · Train · shared groupSample 2 · y=1 · G3 · Train · shared groupSample 3 · y=0 · G4 · Train · shared groupSample 4 · y=1 · G5 · Train · shared groupSample 5 · y=0 · G6 · Train · shared groupSample 6 · y=1 · G7 · Train · shared groupSample 7 · y=0 · G8 · Train · shared groupSample 8 · y=0 · G1 · Train · shared groupSample 9 · y=0 · G2 · Train · shared groupSample 10 · y=1 · G3 · Train · shared groupSample 11 · y=0 · G4 · Train · shared groupSample 12 · y=0 · G5 · Train · shared groupSample 13 · y=0 · G6 · Train · shared groupSample 14 · y=1 · G7 · Train · shared groupSample 15 · y=0 · G8 · Train · shared groupSample 16 · y=1 · G1 · Train · shared groupSample 17 · y=0 · G2 · Train · shared groupSample 18 · y=1 · G3 · Train · shared groupSample 19 · y=0 · G4 · Train · shared groupSample 20 · y=0 · G5 · Train · shared groupSample 21 · y=0 · G6 · Train · shared groupSample 22 · y=0 · G7 · Train · shared groupSample 23 · y=0 · G8 · Train · shared groupSample 24 · y=0 · G1 · Train · shared groupSample 25 · y=0 · G2 · Train · shared groupSample 26 · y=1 · G3 · Train · shared groupSample 27 · y=0 · G4 · Train · shared groupSample 28 · y=1 · G5 · Train · shared groupSample 29 · y=0 · G6 · Train · shared groupSample 30 · y=1 · G7 · Test · shared groupSample 31 · y=0 · G8 · Test · shared groupSample 32 · y=1 · G1 · Test · shared groupSample 33 · y=0 · G2 · Test · shared groupSample 34 · y=1 · G3 · Test · shared groupSample 35 · y=0 · G4 · Test · shared groupSample 36 · y=0 · G5 · Test · shared groupSample 37 · y=0 · G6 · Test · shared groupSample 38 · y=1 · G7 · Test · shared groupSample 39 · y=0 · G8 · Train · shared groupSample 40 · y=0 · G1 · Train · shared groupSample 41 · y=0 · G2 · Train · shared groupSample 42 · y=1 · G3 · Train · shared groupSample 43 · y=0 · G4 · Train · shared groupSample 44 · y=0 · G5 · Train · shared groupSample 45 · y=0 · G6 · Train · shared groupSample 46 · y=0 · G7 · Train · shared groupSample 47 · y=0 · G8 · Train · shared groupFold 5Sample 0 · y=0 · G1 · Train · shared groupSample 1 · y=0 · G2 · Train · shared groupSample 2 · y=1 · G3 · Train · shared groupSample 3 · y=0 · G4 · Train · shared groupSample 4 · y=1 · G5 · Train · shared groupSample 5 · y=0 · G6 · Train · shared groupSample 6 · y=1 · G7 · Train · shared groupSample 7 · y=0 · G8 · Train · shared groupSample 8 · y=0 · G1 · Train · shared groupSample 9 · y=0 · G2 · Train · shared groupSample 10 · y=1 · G3 · Train · shared groupSample 11 · y=0 · G4 · Train · shared groupSample 12 · y=0 · G5 · Train · shared groupSample 13 · y=0 · G6 · Train · shared groupSample 14 · y=1 · G7 · Train · shared groupSample 15 · y=0 · G8 · Train · shared groupSample 16 · y=1 · G1 · Train · shared groupSample 17 · y=0 · G2 · Train · shared groupSample 18 · y=1 · G3 · Train · shared groupSample 19 · y=0 · G4 · Train · shared groupSample 20 · y=0 · G5 · Train · shared groupSample 21 · y=0 · G6 · Train · shared groupSample 22 · y=0 · G7 · Train · shared groupSample 23 · y=0 · G8 · Train · shared groupSample 24 · y=0 · G1 · Train · shared groupSample 25 · y=0 · G2 · Train · shared groupSample 26 · y=1 · G3 · Train · shared groupSample 27 · y=0 · G4 · Train · shared groupSample 28 · y=1 · G5 · Train · shared groupSample 29 · y=0 · G6 · Train · shared groupSample 30 · y=1 · G7 · Train · shared groupSample 31 · y=0 · G8 · Train · shared groupSample 32 · y=1 · G1 · Train · shared groupSample 33 · y=0 · G2 · Train · shared groupSample 34 · y=1 · G3 · Train · shared groupSample 35 · y=0 · G4 · Train · shared groupSample 36 · y=0 · G5 · Train · shared groupSample 37 · y=0 · G6 · Train · shared groupSample 38 · y=1 · G7 · Train · shared groupSample 39 · y=0 · G8 · Test · shared groupSample 40 · y=0 · G1 · Test · shared groupSample 41 · y=0 · G2 · Test · shared groupSample 42 · y=1 · G3 · Test · shared groupSample 43 · y=0 · G4 · Test · shared groupSample 44 · y=0 · G5 · Test · shared groupSample 45 · y=0 · G6 · Test · shared groupSample 46 · y=0 · G7 · Test · shared groupSample 47 · y=0 · G8 · Test · shared groupTrainTestUnusedShared groupEntity numbers identify G1 through G8. A rust underline marks an entity on both sides of a fold.
Folds
5
Total samples
48
Overall positive rate
29.2%
Max fold class drift
18.1 pp
Leaked group pairs
40
Lookahead violations
93
Avg train size
38.4
Avg test size
9.6
Per-fold summary
FoldTrain nTest nTrain pos %Test pos %Leaked groupsStatus
1381028.9%30.0%G1, G2, G3, G4, G5, G6, G7, G8Entity overlap · Lookahead (38)
2381026.3%40.0%G3, G4, G5, G6, G7, G8, G1, G2Entity overlap · Lookahead (28)
3381031.6%20.0%G5, G6, G7, G8, G1, G2, G3, G4Entity overlap · Lookahead (18)
439925.6%44.4%G7, G8, G1, G2, G3, G4, G5, G6Entity overlap · Lookahead (9)
539933.3%11.1%G8, G1, G2, G3, G4, G5, G6, G7Entity overlap

Reproduce this split

Both examples use the displayed samples. Random generators and tie handling can produce different fold indices across libraries. Copy the page URL to share your settings.

import { utils } from '@kanaries/ml';

const y = [0,0,1,0,1,0,1,0,0,0,1,0,0,0,1,0,1,0,1,0,0,0,0,0,0,0,1,0,1,0,1,0,1,0,1,0,0,0,1,0,0,0,1,0,0,0,0,0];
const groups = ["G1","G2","G3","G4","G5","G6","G7","G8","G1","G2","G3","G4","G5","G6","G7","G8","G1","G2","G3","G4","G5","G6","G7","G8","G1","G2","G3","G4","G5","G6","G7","G8","G1","G2","G3","G4","G5","G6","G7","G8","G1","G2","G3","G4","G5","G6","G7","G8"];
const X = y.map((_, i) => [i]);
const cv = new utils.ModelSelection.KFold({"nSplits":5,"shuffle":false});
console.log(cv.split(X));

What will the next prediction look like?

Imagine evaluating a subscription renewal model with twelve weeks of activity per customer. Your product team might care about two different predictions. One is next week’s renewal for someone with an established account. The other is renewal for someone who signed up yesterday. Both use the same outcome column, yet each calls for a different test set. The first evaluation needs a cutoff in time. The second needs customers whose records were absent during training. Decide which question a score should answer before choosing a splitter.

Read the diagram one column at a time. A column represents an observation at a particular time step. Its first two cells give the binary outcome and entity number. Follow the column downward to see when that observation enters training or validation. Moving across a single fold instead shows everything one fitted model gets to learn from. Change only the splitter on your first pass so the rows, labels and entity histories stay the same.

Why a single train/test split gives a noisy score

Suppose a test set contains ten renewals and your model catches seven. That gives a recall of 70 percent. Just one additional correct prediction moves recall by ten percentage points. A lucky draw of straightforward cases can make that change happen even when the training procedure stays identical. Training composition matters too. With a small dataset, moving a handful of unusual accounts into the test set can remove nearly every example of their behavior from training.

KFold divides the rows into disjoint test blocks. Each block takes its turn in validation while the remaining blocks supply training examples. Across the complete run every row receives one out-of-fold prediction. Inspecting the spread of scores reveals how much results vary with the particular cases held out. The training sets overlap, so five fold scores do not constitute five independent experiments. Keep an untouched final evaluation set if you repeatedly consult cross-validation to choose features or tune a model.

KFold and StratifiedKFold on rare outcomes

Set the ordering to grouped, turn shuffle off and lower the positive rate. Examine the class strip above each dark test block. Entities here have different tendencies to produce positive outcomes, so adjacent rows can contain long runs of one label. KFold respects the supplied row order by default. A validation block may capture a pocket of renewals while another contains mostly cancellations. The maximum fold class drift measures the largest gap between a block’s positive fraction and the fraction in the entire dataset.

Switch to StratifiedKFold with the same controls. Class labels guide the allocation so that each test fold receives roughly its share of each outcome. Whole observations impose a rounding limit. Seven positives distributed across five folds cannot give every fold the same positive count. Balancing labels helps when comparing models on rare outcomes, particularly for recall, which needs positive examples in the denominator. Check the entity strip as well. Similar test percentages alone tell you little about whether the validation customers were previously seen.

Hold out the unit that must generalize

A patient’s measurements across visits often resemble one another. A classifier may recognize a stable baseline, a device calibration or a clinic-specific measurement pattern even after a patient identifier is removed from the features. When visits from one patient appear on both sides of a split, recognizing that patient can improve validation performance. The apparent gain may disappear on the first visit from a new patient. The same reasoning applies to clicks from one user or overlapping recording windows from one session.

Use interleaved ordering to bring this effect into view. In a KFold row, find any underlined test cell and read its entity number. Follow the strip sideways to find an underlined training cell for that same entity. The table counts the entity once in that fold even if it contributed many visits. If it crosses the boundary again in the next fold, that contributes another fold/group pair. The underline shows an opportunity for leakage. Measuring the size of the score inflation requires fitting and evaluating a model.

GroupKFold keeps all observations from an entity together and distributes the groups across validation folds. StratifiedGroupKFold additionally considers class counts when assigning groups. Try six groups and six folds. Each test set must hold an entire group, so the splitter has little freedom to correct a group with an unusually high positive rate. Additional independent groups give it more choices. GroupShuffleSplit answers a related question by drawing held-out groups repeatedly. A difficult patient can appear in several draws, and another patient may never enter a test set.

Walk forward for a forecast

For a forecast, reconstruct what could have been known at the moment of prediction. Turning off shuffle in KFold leaves observations in sequence, but look at its first fold. The test block comes first and the training rows lie later on the timeline. The audit counts each training row later than the earliest test time once per fold. That quantity matters if the task requires learning exclusively from prior events. For an exchangeable collection of independently sampled items, column order may have no such interpretation.

TimeSeriesSplit starts with an initial training window and tests on the following block. Move down one strip and the training window expands into observations that have now become historical. Future blocks stay unused until their turn. Here the displayed column position defines synthetic time for either ordering. With production data, sort by the actual event time before splitting. Also check when the target becomes available. A seven-day outcome for an event immediately before the cutoff may still be unknown at training time. Leave an appropriate gap for that delay.

Fit preprocessing inside each fold

Once the indices look sensible, apply them to the entire learning procedure. Consider filling missing purchase amounts with a median. Computing that median on the full table allows held-out purchases to influence a training decision. Put the imputer, scaler and estimator into a Pipeline and fit a fresh pipeline on the training indices of each fold. During validation, use the median and scale learned from those training rows. Apply the same discipline to supervised feature selection, where consulting validation labels can have an even larger effect.

Save the fold indices alongside a stable dataset version when comparing two pipelines. Both candidates then face the same held-out cases, and a difference in recall can be traced to specific positive observations. Before reporting an average, inspect each fold’s test size, class fraction and distinct entities. A large fold with common outcomes and a small fold with rare outcomes may contribute very different evidence. Document the prediction population and time horizon with the score so the next reader knows where that evidence applies.

Frequently asked questions

Questions about cross-validation split visualizer

Which splitter should I use for repeated patients?

To estimate performance on new patients, use patient ID as the group and hold out entire patients with GroupKFold. StratifiedGroupKFold also tries to distribute diagnoses across folds.

Why can a stratified split still leak?

StratifiedKFold uses labels to allocate rows. Two visits from the same person can have the same diagnosis and land on opposite sides of a fold.

Why are some cells unused?

TimeSeriesSplit leaves later observations outside the current training and testing windows. The GroupShuffleSplit example explicitly reserves one unused group per draw through its train and test sizes.

What does class drift in percentage points mean?

If 30% of all rows are positive and one test fold has 45% positives, that fold has 15 percentage points of drift. The dashboard reports the largest absolute difference.

Can the same row appear in multiple test sets?

Yes for GroupShuffleSplit, which makes repeated draws of groups. In KFold, GroupKFold and their stratified counterparts, every row is tested exactly once per complete run.

How do I reproduce a split outside the browser?

Copy the JavaScript example for the current settings or download the fold indices. Keep your data in the same row order. Use the exported indices for identical assignments across languages.

Powered by @kanaries/ml — the scikit-learn-style ML library for JavaScriptnpmGitHub