cross_val
Builtin index · Statistics & ML
result = cross_val(fit, X, y, [extra...], [folds=5], [shuffle=true], [seed=], [metric="auto"])
Input: fit, X, y, extra..., folds, shuffle, seed, metric · Output: result
What it is
k-fold cross-validation of anything that fits a model. fit is a function you wrote, fit(X, y, extra...), returning a model; or a model builder passed by name, such as knn_model, with its extra arguments after y (cross_val(knn_model, X, y, 3)). Each fold fits on the other folds and scores on the held-out one: by the model's own score for metric="auto", else by the named metric, one of accuracy, f1, precision, recall, r2, rmse, mae, mse, roc_auc, brier, on (actual, predict(X)).
Overloads
One call shape — this function is not overloaded.
| Code | Input | Output | Description |
result = cross_val(fit, X, y, [extra...], [folds=5], [shuffle=true], [seed=], [metric="auto"]) | fit —fit is a function you wrote, fit(X, y, extra...), returning a model.X —fit is a function you wrote, fit(X, y, extra...), returning a model.y —fit is a function you wrote, fit(X, y, extra...), returning a model.extra... optional, repeatablefit is a function you wrote, fit(X, y, extra...), returning a model.folds optional (default 5)Each fold fits on the other folds and scores on the held-out one: by the model's own score for metric="auto", else by the named metric, one of accuracy, f1, precision, recall, r2, rmse, mae, mse, roc_auc, brier, on (actual, predict(X)).shuffle optional (default true)not documented yetseed optionalseed= fixes the folds.metric optional (default "auto")Each fold fits on the other folds and scores on the held-out one: by the model's own score for metric="auto", else by the named metric, one of accuracy, f1, precision, recall, r2, rmse, mae, mse, roc_auc, brier, on (actual, predict(X)). | result — | Returns {scores, mean, std, folds, metric}; std is the population standard deviation. seed= fixes the folds. |
Examples
No documentation example calls this form yet. The Statistics & ML chapter is where one belongs.
How it works
train_test_split(X, y, [test_size=0.2], [seed=], [shuffle=true]) — scikit-learn's train_test_split: a random row permutation (shuffle=true, the default) split into a leading train fraction and a trailing test fraction, X/y permuted in lockstep so no row's features and label ever separate. An explicit seed= gives a fresh reproducible local RNG stream (two calls with the same seed= produce byte-identical splits); omitted, it advances the interpreter's persistent stream instead — the same fork randn's own doc comment describes, not a new convention. shuffle=false keeps the original row order (see sequential_split for why that's also exposed under its own name). Returns a "split" model handle with .X_train/.X_test/.y_train/.y_test fields. cross_val(fit, X, y, [extra...], [folds=5], [shuffle=true], [seed=], [metric=]) -- k-fold cross-validation of anything that fits a model. fit(X, y, extra...) is a function you wrote (or a model builder such as knn_model passed by name, with its extra arguments after y); each fold fits on the other folds and scores on the held-out one.
From the note above this builtin in the interpreter, where the reasoning was written down as it was built.