train_val_test_split
Builtin index · Statistics & ML
result = train_val_test_split(X, y, [val_size=0.2], [test_size=0.2], [seed=], [shuffle=true])
Input: X, y, val_size, test_size, seed, shuffle · Output: result
What it is
Three-way split of X/y into training, validation, and test sets: val_size/test_size are numbers in (0, 1) (both default 0.2); val_size is rescaled against the remainder left after removing the test set, so the final validation share really is what you asked for rather than a share of a share.
Overloads
One call shape — this function is not overloaded.
| Code | Input | Output | Description |
result = train_val_test_split(X, y, [val_size=0.2], [test_size=0.2], [seed=], [shuffle=true]) | X numberThree-way split of X/y into training, validation, and test sets: val_size/test_size are numbers in (0, 1) (both default 0.2).y numberThree-way split of X/y into training, validation, and test sets: val_size/test_size are numbers in (0, 1) (both default 0.2).val_size optional (default 0.2)val_size is rescaled against the remainder left after removing the test set, so the final validation share really is what you asked for rather than a share of a share.test_size number, optional (default 0.2)Three-way split of X/y into training, validation, and test sets: val_size/test_size are numbers in (0, 1) (both default 0.2).seed optionalnot documented yetshuffle optional (default true)not documented yet | X_test —matricesy_test vectorvectors | Returns a Record with X_train/X_val/X_test (matrices) and y_train/y_val/y_test (vectors). |
Examples
From the documentation, and run by the build -- the output and the figure below are what the engine actually produced. Simplest first.
seq = sequential_split(X, y, test_size = 0.2)
print("sequential_split train rows: {rows(seq.X_train)}")
strat = stratified_split(Xc, yc, test_size = 0.3, seed = 9)
print("stratified_split train rows: {rows(strat.X_train)}")
tvt = train_val_test_split(Xc, yc, val_size = 0.2, test_size = 0.2, seed = 9)
print("train/val/test rows: {rows(tvt.X_train)}, {rows(tvt.X_val)}, {rows(tvt.X_test)}")
m = knn_model(strat.X_train, strat.y_train, 5, kind = "classification")
confusion_matrix(strat.y_test, m.predict(strat.X_test), 2)
sequential_split train rows: 96 stratified_split train rows: 140 train/val/test rows: 120, 40, 40
from statistics-ml
How it works
train_val_test_split(X, y, [val_size=0.2], [test_size=0.2], [seed=], [shuffle=true]) — the three-way split hyperparameter tuning needs: a validation set to select/tune on plus a final held-out test set that's never touched until the very end. Implemented as two sequential splits done carefully: X/y are permuted once (same seed=/shuffle= contract as train_test_split), test_size peels the trailing test fraction off the whole dataset first, and val_size then peels its share off the REMAINING (1-test_size) pool — rescaled to val_size / (1-test_size) of that remainder, not val_size of it directly, so the *final* validation fraction of the whole dataset actually equals val_size as requested. (The naive version — val_size of what's left after removing the test set, unrescaled — silently under-delivers: e.g. val_size=0.2 after a test_size=0.2 cut leaves a pool of 80% of the data, and taking a naive 20% of *that* pool is only 16% of the original dataset, not the requested 20%.) Returns a "split3" model handle with .X_train/.X_val/.X_test/ .y_train/.y_val/.y_test fields.
From the note above this builtin in the interpreter, where the reasoning was written down as it was built.