Train Test Split Calculator
Enter a dataset size and a ratio to get the exact integer sample count for train, validation, and test — or per-fold sizes for k-fold cross-validation. Counts follow scikit-learn's own rounding, so they match what your code produces. Runs fully in your browser, no signup.
How it works
A machine-learning dataset is split into parts so a model can be trained, tuned, and evaluated on data it has not seen. The hard part is not the ratio — it is the rounding. A 15% test set of 1,003 rows is not 150.45 rows; it has to be a whole number, and different libraries round differently. This tool reproduces scikit-learn's conventions exactly so the counts you report match the counts your code produces.
Two-way split (Train / Test). Given N samples and a test fraction t, scikit-learn's train_test_split computes n_test = ceil(t × N) then n_train = N − n_test. The ceiling — not a plain round — is why a naive percentage can be off by one sample. A round-half-up option is offered for tutorials that round instead.
Three-way split (Train / Val / Test). This is done as two sequential splits, the same pattern as calling train_test_split twice. First the test set is removed: n_test = ceil(test_f × N), leaving a remainder R = N − n_test. The validation fraction is then taken relative to the remainder, val_rel = val_f / (1 − test_f), so that n_val = ceil(val_rel × R) and n_train = R − n_val. Reporting each part as count ÷ N recovers the realised percentages.
k-fold cross-validation. scikit-learn's KFold sets base = ⌊N / k⌋ and r = N mod k. The first r folds each hold base + 1 samples and the rest hold base. When a fold is used as the test set, the other k−1 folds train, so train size = N − fold size. The fold sizes always sum to N, and every sample is in the test fold exactly once — total test coverage equals N.
One subtlety this tool handles: floating-point error. In IEEE-754 doubles, 0.15 × 1000 evaluates to 150.00000000000003, and a raw ceiling would return 151 — off by one from scikit-learn. The calculator snaps any product within a tiny tolerance of a whole number back to that integer before taking the ceiling, which keeps the counts correct even for very large datasets.
Worked examples
Frequently asked questions
Sources & references
- scikit-learn — train_test_split API reference (n_test = ceil(test_size × N))
- scikit-learn — KFold API reference (fold-size distribution)
- scikit-learn — Cross-validation user guide (train/validation/test protocol)
The rounding conventions on this page were last cross-checked against the scikit-learn documentation on 2026-07-09. The three worked examples above are reproduced exactly by the calculator.
Related tools
Comments & feedback
Spotted a bug or want an improvement? Tell us — our team reviews every comment, and good ideas get built. Comments are public and anonymous.
Found a bug, edge case, or want to suggest an improvement?
Email me at [email protected] — most fixes ship within 24 hours.