mojolearn.cross_val_score#

mojolearn.cross_val_score(estimator, X, y, *, cv=None, scoring=None, groups=None, n_jobs=1, error_score='raise')[source]#

Evaluate an estimator by cross-validation and return one score per fold.

For each fold, a fresh clone of estimator is fit on the training rows and scored on the held-out rows. Folds run one after another. Every fold is checked before the first fit: each side must be nonempty, have no repeated rows, and not overlap the other side. A row may be held out in more than one fold.

The default folds are unshuffled, so the scores depend on the order of the rows in X and y. Record split_descriptor(X, y, estimator=estimator, cv=cv) beside the scores if the run must be reproducible.

Parameters:
  • estimator (estimator) – An unfitted estimator with get_params, fit and (when scoring is None) score. A scikit-learn pipeline works too; put unfitted transforms inside it so they are fit on each training fold.

  • X (array-like of shape (n_samples, n_features)) – Dense 2-D training data. Its dtype is kept; the estimator checks the dtypes it supports. Sparse input is not supported.

  • y (array-like of shape (n_samples,)) – 1-D targets matching X’s rows.

  • cv (int, splitter or iterable of (train, test), default None) – None means five folds. An integer gives that many unshuffled folds, stratified by class for a classifier with discrete labels, plain contiguous folds otherwise. A splitter (any object with split(X, y, groups)) or an iterable of integer index pairs is used as given.

  • scoring (str, callable or None, default None) – None calls the fitted estimator’s score. A string is one of get_scorer_names(). A callable is called as scoring(estimator, X_test, y_test) and must return a real scalar. Loss scorers are negated ('neg_mean_squared_error'), so higher is always better.

  • groups (array-like of shape (n_samples,), default None) – Group labels for a splitter that reads them. Passing groups with an integer or default cv raises ValueError, because the default folds would ignore them.

  • n_jobs (int, default 1) – Only 1 is supported; other values raise NotImplementedError.

  • error_score (‘raise’, default ‘raise’) – Only 'raise' is supported: an error in a fit or score propagates.

Returns:

scores (Array of shape (n_splits,), float64) – The score of each fold, in fold order, with no aggregation.

Notes

Fit parameters, sample weights, eval_set, precomputed kernels and refitting are not supported. Set numeric_mode explicitly on every pipeline step and custom metric when the results must be reproducible across devices.

Examples

>>> import numpy as np, mojolearn
>>> from mojolearn.model_selection import cross_val_score
>>> rng = np.random.default_rng(0)
>>> X = rng.random((1000, 8), dtype=np.float32)
>>> y = rng.random(1000, dtype=np.float32)
>>> scores = cross_val_score(mojolearn.Ridge(alpha=1.0), X, y, cv=5)