mojolearn.model_selection#
Cross-validation, splitters and hyperparameter search for mojolearn estimators.
This module follows scikit-learn’s sklearn.model_selection: the splitters
(KFold, StratifiedKFold, GroupKFold, ShuffleSplit, …),
train_test_split, cross_val_score, cross_validate,
cross_val_predict, GridSearchCV, RandomizedSearchCV,
validation_curve, learning_curve, permutation_test_score and the
scorer helpers. scikit-learn is not required; scikit-learn pipelines,
splitters and estimators that follow its protocol also work.
Behavior to know:
Fits run serially, one fold at a time.
n_jobsmust be1(orNonewhere scikit-learn accepts it).Splitters yield
(train, test)pairs of int64 row-index arrays.Shuffled splits draw from mojolearn’s own counter-based random generator, so a given
random_stategives a reproducible split, but not the same rows scikit-learn would pick for that seed.Fold rows are gathered on the device, and X and y stay resident there across folds and candidates. All learning happens in the estimator.
Functions
|
Evaluate an estimator by cross-validation and return one score per fold. |
|
Describe a cross-validation split so a run can be reproduced. |
|
Split arrays into random train and test subsets. |
|
Turn a |
|
Evaluate one or more scores by cross-validation, with fit and score times. |
|
Return each row's prediction from the fold that held it out. |
|
Training and test scores for each value of one parameter. |
|
Training and test scores for growing training-set sizes. |
|
Test the significance of a cross-validated score by permuting y. |
|
Return the scorer for a scoring argument. |
|
Make a scorer from a metric function. |
Return the sorted list of scorer names that |
Classes
|
K-fold cross-validator. |
|
Stratified k-fold cross-validator. |
|
K-fold cross-validator that keeps each group in a single fold. |
|
Stratified k-fold cross-validator that keeps each group in a single fold. |
|
Time-series cross-validator with expanding training windows. |
|
Random permutation cross-validator. |
|
Stratified random-permutation cross-validator. |
|
Shuffle-split cross-validator over groups. |
Leave-one-out cross-validator. |
|
|
Leave-P-out cross-validator. |
Leave-one-group-out cross-validator. |
|
|
Leave-P-groups-out cross-validator. |
|
Repeated k-fold cross-validator. |
|
Repeated stratified k-fold cross-validator. |
|
Cross-validator with user-defined folds. |
|
Grid of parameter combinations, iterated as dicts. |
|
Random sample of parameter combinations, iterated as dicts. |
|
Exhaustive search over a parameter grid by cross-validation. |
|
Randomized search over parameter lists by cross-validation. |
- mojolearn.model_selection.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
estimatoris 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,fitand (whenscoringis 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 ofget_scorer_names(). A callable is called asscoring(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
groupswith an integer or defaultcvraisesValueError, because the default folds would ignore them.n_jobs (int, default 1) – Only
1is supported; other values raiseNotImplementedError.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. Setnumeric_modeexplicitly 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)
- mojolearn.model_selection.split_descriptor(X, y, *, estimator=None, cv=None, groups=None)[source]#
Describe a cross-validation split so a run can be reproduced.
The scores from
cross_val_scoredepend on the order of the rows inXandy: the default folds are unshuffled, so the fold a row lands in is decided by its position. mojolearn never reorders your data, so it cannot tell two orderings of the same rows apart. Record this descriptor beside the scores; it hashes the data in arrival order and the fold assignment, so a reader can check that a rerun used the same split.The descriptor covers the split only. It does not hash the scores, the estimator or the numeric mode.
- Parameters:
X (array-like of shape (n_samples, n_features)) – Dense, nonempty 2-D training data, in the order passed to
cross_val_score.y (array-like of shape (n_samples,)) – Targets, in the same order. A buffer array or a list of labels.
estimator (estimator, default None) – The estimator given to
cross_val_score. Required whencvis None or an integer, because the default folds are stratified for a classifier and plainKFoldotherwise; omitting it then raisesValueError.cv (int, splitter or iterable of (train, test), default None) – The
cvgiven tocross_val_score(None means five folds).groups (array-like of shape (n_samples,), default None) – Group labels, when the splitter reads them.
- Returns:
descriptor (dict) – A JSON-serializable dict with keys
schema,n_rows,n_features,X_sha256,y_sha256,groups_sha256,fold_assignment_sha256,n_folds,fold_sizes(one[n_train, n_test]pair per fold) andsha256, a hash over the canonical encoding of every other field.X_sha256andy_sha256change when the rows are reordered, even when the fold indices do not.- Raises:
ValueError – If
estimatoris missing for an integer or defaultcv, if X is not a nonempty 2-D array, if y does not match X’s rows, or ifcvproduces no folds.
Examples
>>> import numpy as np, mojolearn >>> from mojolearn.model_selection import cross_val_score, split_descriptor >>> rng = np.random.default_rng(0) >>> X = rng.random((1000, 8), dtype=np.float32) >>> y = rng.random(1000, dtype=np.float32) >>> model = mojolearn.Ridge(alpha=1.0) >>> scores = cross_val_score(model, X, y) >>> record = split_descriptor(X, y, estimator=model)
- class mojolearn.model_selection.KFold(n_splits=5, *, shuffle=False, random_state=None)[source]#
Bases:
_KFoldBaseK-fold cross-validator.
Splits the rows into
n_splitscontiguous folds of the (optionally shuffled) row order; each fold is the test set once. The firstn_samples % n_splitsfolds are one row larger. Follows scikit-learn’sKFold; shuffling uses mojolearn’s counter-based random generator, so the shuffled folds differ from scikit-learn’s for the same seed.- Parameters:
n_splits (int, default 5) – Number of folds, at least 2.
shuffle (bool, default False) – Shuffle the rows before splitting into folds.
random_state (int or None, default None) – Seed for the shuffle. Setting it with
shuffle=FalseraisesValueError, as in scikit-learn.
Examples
>>> import numpy as np >>> from mojolearn.model_selection import KFold >>> X = np.random.default_rng(0).random((1000, 8), dtype=np.float32) >>> for train, test in KFold(n_splits=5, shuffle=True, random_state=0).split(X): ... pass
- class mojolearn.model_selection.StratifiedKFold(n_splits=5, *, shuffle=False, random_state=None)[source]#
Bases:
_KFoldBaseStratified k-fold cross-validator.
Each fold keeps roughly the class proportions of
y. Follows scikit-learn’sStratifiedKFold: classes are taken in order of first appearance and allocated round-robin over the class-sorted labels. Withshuffle=Trueeach class’s fold assignment is permuted by mojolearn’s counter-based random generator, so shuffled folds differ from scikit-learn’s for the same seed.- Parameters:
n_splits (int, default 5) – Number of folds, at least 2.
shuffle (bool, default False) – Shuffle each class’s rows before assigning folds.
random_state (int or None, default None) – Seed for the shuffle. Setting it with
shuffle=FalseraisesValueError.
Examples
>>> import numpy as np >>> from mojolearn.model_selection import StratifiedKFold >>> rng = np.random.default_rng(0) >>> X = rng.random((1000, 8), dtype=np.float32) >>> y = rng.integers(0, 3, 1000).astype(np.int32) >>> folds = list(StratifiedKFold(n_splits=5).split(X, y))
- class mojolearn.model_selection.GroupKFold(n_splits=5, *, shuffle=False, random_state=None)[source]#
Bases:
_KFoldBaseK-fold cross-validator that keeps each group in a single fold.
Follows scikit-learn’s
GroupKFold. Unshuffled, groups are assigned largest first, each to the fold with the fewest rows so far (the lowest fold index on a tie). Shuffled, the groups are permuted and split inton_splitsruns of nearly equal group count.- Parameters:
n_splits (int, default 5) – Number of folds, at least 2 and at most the number of groups.
shuffle (bool, default False) – Shuffle the groups before splitting.
random_state (int or None, default None) – Seed for the shuffle (mojolearn’s counter-based generator). Setting it with
shuffle=FalseraisesValueError.
Examples
>>> import numpy as np >>> from mojolearn.model_selection import GroupKFold >>> rng = np.random.default_rng(0) >>> X = rng.random((1000, 8), dtype=np.float32) >>> groups = rng.integers(0, 20, 1000) >>> folds = list(GroupKFold(n_splits=5).split(X, groups=groups))
- class mojolearn.model_selection.StratifiedGroupKFold(n_splits=5, *, shuffle=False, random_state=None)[source]#
Bases:
_KFoldBaseStratified k-fold cross-validator that keeps each group in a single fold.
Follows scikit-learn’s
StratifiedGroupKFold: groups are taken in descending order of the standard deviation of their class distribution (or in a shuffled order), and each goes to the fold whose class distribution it changes least.- Parameters:
n_splits (int, default 5) – Number of folds, at least 2.
shuffle (bool, default False) – Shuffle the group order instead of sorting it.
random_state (int or None, default None) – Seed for the shuffle (mojolearn’s counter-based generator). Setting it with
shuffle=FalseraisesValueError.
Examples
>>> import numpy as np >>> from mojolearn.model_selection import StratifiedGroupKFold >>> rng = np.random.default_rng(0) >>> X = rng.random((1000, 8), dtype=np.float32) >>> y = rng.integers(0, 2, 1000) >>> groups = rng.integers(0, 50, 1000) >>> folds = list(StratifiedGroupKFold(n_splits=5).split(X, y, groups))
- class mojolearn.model_selection.TimeSeriesSplit(n_splits=5, *, max_train_size=None, test_size=None, gap=0)[source]#
Bases:
_SplitterTime-series cross-validator with expanding training windows.
Follows scikit-learn’s
TimeSeriesSplit. Splititests on a block of consecutive rows and trains on the rows before it, so a model never trains on rows that come after its test block. There is no randomness.- Parameters:
n_splits (int, default 5) – Number of splits, at least 2.
max_train_size (int or None, default None) – Largest training window; older rows are dropped beyond it.
test_size (int or None, default None) – Rows per test block. None means
n_samples // (n_splits + 1).gap (int, default 0) – Rows left out between the end of each training window and its test block.
Examples
>>> import numpy as np >>> from mojolearn.model_selection import TimeSeriesSplit >>> X = np.random.default_rng(0).random((1000, 8), dtype=np.float32) >>> folds = list(TimeSeriesSplit(n_splits=5, gap=10).split(X))
- class mojolearn.model_selection.ShuffleSplit(n_splits=10, *, test_size=None, train_size=None, random_state=None)[source]#
Bases:
_SplitterRandom permutation cross-validator.
Each split draws a fresh random permutation of the rows; its first
n_testrows are the test set and the nextn_trainrows the training set. Splits may overlap. Follows scikit-learn’sShuffleSplit; the permutations come from mojolearn’s counter-based random generator, so the rows differ from scikit-learn’s for the same seed.- Parameters:
n_splits (int, default 10) – Number of splits.
test_size (float, int or None, default None) – A float in (0, 1) is a fraction of the rows (rounded up), an int an absolute count. None means the complement of
train_size, or 0.1 when both are None.train_size (float, int or None, default None) – A float in (0, 1) is a fraction of the rows (rounded down), an int an absolute count. None means the complement of
test_size.random_state (int or None, default None) – Seed for the permutations.
Examples
>>> import numpy as np >>> from mojolearn.model_selection import ShuffleSplit >>> X = np.random.default_rng(0).random((1000, 8), dtype=np.float32) >>> folds = list(ShuffleSplit(n_splits=5, test_size=0.2, random_state=0).split(X))
- class mojolearn.model_selection.StratifiedShuffleSplit(n_splits=10, *, test_size=None, train_size=None, random_state=None)[source]#
Bases:
ShuffleSplitStratified random-permutation cross-validator.
Each split keeps roughly the class proportions of
yin both the training and the test set. Follows scikit-learn’sStratifiedShuffleSplit: each class’s rows are permuted and the per-class train and test counts are scikit-learn’s approximate mode of the class proportions; the training and test rows are then shuffled. The permutations come from mojolearn’s counter-based random generator.- Parameters:
n_splits (int, default 10) – Number of splits.
test_size (float, int or None, default None) – Fraction or number of rows in the test set. None means the complement of
train_size, or 0.1 when both are None.train_size (float, int or None, default None) – Fraction or number of rows in the training set. None means the complement of
test_size.random_state (int or None, default None) – Seed for the permutations.
Examples
>>> import numpy as np >>> from mojolearn.model_selection import StratifiedShuffleSplit >>> rng = np.random.default_rng(0) >>> X = rng.random((1000, 8), dtype=np.float32) >>> y = rng.integers(0, 3, 1000) >>> cv = StratifiedShuffleSplit(n_splits=5, test_size=0.2, random_state=0) >>> folds = list(cv.split(X, y))
- class mojolearn.model_selection.GroupShuffleSplit(n_splits=10, *, test_size=None, train_size=None, random_state=None)[source]#
Bases:
ShuffleSplitShuffle-split cross-validator over groups.
Works like
ShuffleSpliton the sorted distinct groups, and every row follows its group, so a group is never split between train and test.test_sizeandtrain_sizecount groups, not rows. Follows scikit-learn’sGroupShuffleSplit.- Parameters:
n_splits (int, default 10) – Number of splits.
test_size (float, int or None, default None) – Fraction or number of groups in the test set. None means the complement of
train_size, or 0.2 when both are None.train_size (float, int or None, default None) – Fraction or number of groups in the training set. None means the complement of
test_size.random_state (int or None, default None) – Seed for the permutations (mojolearn’s counter-based generator).
Examples
>>> import numpy as np >>> from mojolearn.model_selection import GroupShuffleSplit >>> rng = np.random.default_rng(0) >>> X = rng.random((1000, 8), dtype=np.float32) >>> groups = rng.integers(0, 50, 1000) >>> cv = GroupShuffleSplit(n_splits=5, test_size=0.2, random_state=0) >>> folds = list(cv.split(X, groups=groups))
- class mojolearn.model_selection.LeaveOneOut[source]#
Bases:
_SplitterLeave-one-out cross-validator.
Each row is the test set once, with every other row as the training set. Follows scikit-learn’s
LeaveOneOut. It takes no parameters.Examples
>>> import numpy as np >>> from mojolearn.model_selection import LeaveOneOut >>> X = np.random.default_rng(0).random((20, 4), dtype=np.float32) >>> folds = list(LeaveOneOut().split(X))
- class mojolearn.model_selection.LeavePOut(p)[source]#
Bases:
_SplitterLeave-P-out cross-validator.
Every combination of
prows is the test set once, in lexicographic order, with the remaining rows as the training set. Follows scikit-learn’sLeavePOut. The number of splits grows asn_samples choose p.- Parameters:
p (int) – Size of each test set; must be less than the number of rows.
Examples
>>> import numpy as np >>> from mojolearn.model_selection import LeavePOut >>> X = np.random.default_rng(0).random((10, 4), dtype=np.float32) >>> folds = list(LeavePOut(2).split(X))
- class mojolearn.model_selection.LeaveOneGroupOut[source]#
Bases:
_SplitterLeave-one-group-out cross-validator.
Each distinct group is the test set once, in sorted group order, with the rows of every other group as the training set. Follows scikit-learn’s
LeaveOneGroupOut. It takes no parameters.Examples
>>> import numpy as np >>> from mojolearn.model_selection import LeaveOneGroupOut >>> rng = np.random.default_rng(0) >>> X = rng.random((1000, 8), dtype=np.float32) >>> groups = rng.integers(0, 5, 1000) >>> folds = list(LeaveOneGroupOut().split(X, groups=groups))
- class mojolearn.model_selection.LeavePGroupsOut(n_groups)[source]#
Bases:
_SplitterLeave-P-groups-out cross-validator.
Every combination of
n_groupsdistinct groups is the test set once, with the rows of the other groups as the training set. Follows scikit-learn’sLeavePGroupsOut.- Parameters:
n_groups (int) – Number of groups in each test set; must be less than the number of distinct groups.
Examples
>>> import numpy as np >>> from mojolearn.model_selection import LeavePGroupsOut >>> rng = np.random.default_rng(0) >>> X = rng.random((1000, 8), dtype=np.float32) >>> groups = rng.integers(0, 5, 1000) >>> folds = list(LeavePGroupsOut(n_groups=2).split(X, groups=groups))
- class mojolearn.model_selection.RepeatedKFold(*, n_splits=5, n_repeats=10, random_state=None)[source]#
Bases:
_RepeatedRepeated k-fold cross-validator.
Runs a shuffled
KFoldn_repeatstimes, each repeat with a different shuffle. Follows scikit-learn’sRepeatedKFold; the shuffles come from mojolearn’s counter-based random generator.- Parameters:
n_splits (int, default 5) – Number of folds per repeat, at least 2.
n_repeats (int, default 10) – Number of repeats, at least 1.
random_state (int or None, default None) – Seed that fixes every repeat’s shuffle.
Examples
>>> import numpy as np >>> from mojolearn.model_selection import RepeatedKFold >>> X = np.random.default_rng(0).random((1000, 8), dtype=np.float32) >>> folds = list(RepeatedKFold(n_splits=5, n_repeats=3, random_state=0).split(X))
- class mojolearn.model_selection.RepeatedStratifiedKFold(*, n_splits=5, n_repeats=10, random_state=None)[source]#
Bases:
_RepeatedRepeated stratified k-fold cross-validator.
Runs a shuffled
StratifiedKFoldn_repeatstimes, each repeat with a different shuffle. Follows scikit-learn’sRepeatedStratifiedKFold; the shuffles come from mojolearn’s counter-based random generator.- Parameters:
n_splits (int, default 5) – Number of folds per repeat, at least 2.
n_repeats (int, default 10) – Number of repeats, at least 1.
random_state (int or None, default None) – Seed that fixes every repeat’s shuffle.
Examples
>>> import numpy as np >>> from mojolearn.model_selection import RepeatedStratifiedKFold >>> rng = np.random.default_rng(0) >>> X = rng.random((1000, 8), dtype=np.float32) >>> y = rng.integers(0, 2, 1000) >>> cv = RepeatedStratifiedKFold(n_splits=5, n_repeats=3, random_state=0) >>> folds = list(cv.split(X, y))
- class mojolearn.model_selection.PredefinedSplit(test_fold)[source]#
Bases:
_SplitterCross-validator with user-defined folds.
test_fold[i]is the fold in which rowiis tested; a value of -1 keeps that row in every training set and never tests it. Folds are yielded in sorted order of their values. Follows scikit-learn’sPredefinedSplit.- Parameters:
test_fold (array-like of int, shape (n_samples,)) – The fold of each row, or -1.
Examples
>>> import numpy as np >>> from mojolearn.model_selection import PredefinedSplit >>> cv = PredefinedSplit(np.array([0, 1, -1, 1, 0])) >>> folds = list(cv.split())
- mojolearn.model_selection.train_test_split(*arrays, test_size=None, train_size=None, random_state=None, shuffle=True, stratify=None)[source]#
Split arrays into random train and test subsets.
Follows scikit-learn’s
train_test_split. Withshuffle=Truethe rows are chosen by oneShuffleSplitdraw (orStratifiedShuffleSplitwhenstratifyis given) from mojolearn’s counter-based random generator, so the rows differ from scikit-learn’s for the same seed. Withshuffle=Falsethe leading rows are the training set and the rows after them the test set.- Parameters:
*arrays (sequence of array-likes) – Arrays or lists with the same number of rows. Buffer arrays are returned as mojolearn
Arrayobjects, lists as lists.test_size (float, int or None, default None) – Fraction (rounded up) or number of rows in the test set. None means the complement of
train_size, or 0.25 when both are None.train_size (float, int or None, default None) – Fraction (rounded down) or number of rows in the training set. None means the complement of
test_size.random_state (int or None, default None) – Seed for the shuffle.
shuffle (bool, default True) – Whether to shuffle before splitting.
stratify (array-like or None, default None) – Class labels to stratify by. Requires
shuffle=True; withshuffle=Falseit raisesValueError.
- Returns:
splitting (list) – Two pieces per input, in scikit-learn’s order:
X_train, X_test, y_train, y_test, ....- Raises:
ValueError – If no array is given, the arrays have different numbers of rows, or the sizes are out of range.
Examples
>>> import numpy as np >>> from mojolearn.model_selection import train_test_split >>> rng = np.random.default_rng(0) >>> X = rng.random((1000, 8), dtype=np.float32) >>> y = rng.random(1000, dtype=np.float32) >>> X_train, X_test, y_train, y_test = train_test_split( ... X, y, test_size=0.2, random_state=0)
- mojolearn.model_selection.check_cv(cv=5, y=None, *, classifier=False, shuffle=False, random_state=None)[source]#
Turn a
cvargument into a splitter.Follows scikit-learn’s
check_cv. An integer (or None, meaning 5) becomesStratifiedKFoldwhenclassifieris true andyholds discrete labels (strings, or integer-valued finite numbers), andKFoldotherwise. An object with asplitmethod is returned as is, and an iterable of(train, test)index pairs is wrapped in a splitter that yields those pairs.- Parameters:
cv (int, splitter, iterable or None, default 5) – The cross-validation strategy.
y (array-like of shape (n_samples,), default None) – Targets, read only to decide whether to stratify.
classifier (bool, default False) – Whether the estimator is a classifier.
shuffle (bool, default False) – Passed to the
KFoldorStratifiedKFoldbuilt from an integer.random_state (int or None, default None) – Seed for that splitter, used only when
shuffleis true.
- Returns:
splitter – An object with
split(X, y, groups)andget_n_splits.- Raises:
ValueError – If
cvis a string or another value that is not an integer, splitter or iterable.
Examples
>>> import numpy as np >>> from mojolearn.model_selection import check_cv >>> y = np.random.default_rng(0).integers(0, 2, 1000) >>> cv = check_cv(5, y, classifier=True)
- mojolearn.model_selection.cross_validate(estimator, X, y=None, *, groups=None, scoring=None, cv=None, n_jobs=None, return_train_score=False, return_estimator=False, return_indices=False, error_score=nan)[source]#
Evaluate one or more scores by cross-validation, with fit and score times.
Follows scikit-learn’s
cross_validate. For each fold, a fresh clone ofestimatoris fit on the training rows and scored on the held-out rows. Folds run one after another.- Parameters:
estimator (estimator) – An unfitted estimator with
get_paramsandfit.X (array-like of shape (n_samples, n_features)) – Training data.
y (array-like of shape (n_samples,), default None) – Targets; None for unsupervised estimators.
groups (array-like of shape (n_samples,), default None) – Group labels for a splitter that reads them.
scoring (str, callable, list, tuple, set, dict or None, default None) – None uses the estimator’s
score. A name or callable gives one score, stored astest_score. A list, tuple or set of names, or a dict of name to scorer, gives several, stored astest_<name>.cv (int, splitter, iterable or None, default None) – As in
check_cv: None means five folds, stratified for a classifier.n_jobs (int or None, default None) – Only None or 1 is supported; other values raise
NotImplementedError.return_train_score (bool, default False) – Also score each fold’s training rows (
train_<name>).return_estimator (bool, default False) – Also return the fitted estimator of each fold.
return_indices (bool, default False) – Also return each fold’s train and test indices.
error_score (‘raise’ or float, default nan) –
'raise'propagates a fit error; a number is recorded as the fold’s score instead.
- Returns:
results (dict) –
fit_time,score_time(wall-clock seconds) and the score arrays, each a float64Arrayof shape (n_splits,); plusestimator(a list) andindices({'train': [...], 'test': [...]}) when requested.
Examples
>>> import numpy as np, mojolearn >>> from mojolearn.model_selection import cross_validate >>> rng = np.random.default_rng(0) >>> X = rng.random((1000, 8), dtype=np.float32) >>> y = rng.random(1000, dtype=np.float32) >>> res = cross_validate(mojolearn.Ridge(alpha=1.0), X, y, cv=5, ... scoring=['r2', 'neg_mean_squared_error'])
- mojolearn.model_selection.cross_val_predict(estimator, X, y=None, *, groups=None, cv=None, n_jobs=None, method='predict')[source]#
Return each row’s prediction from the fold that held it out.
Follows scikit-learn’s
cross_val_predict. For each fold, a fresh clone ofestimatoris fit on the training rows and itsmethodis called on the held-out rows; the predictions are put back in row order. The folds must hold out every row exactly once, otherwiseValueErroris raised.- Parameters:
estimator (estimator) – An unfitted estimator with
get_params,fitandmethod.X (array-like of shape (n_samples, n_features)) – Training data.
y (array-like of shape (n_samples,), default None) – Targets.
groups (array-like of shape (n_samples,), default None) – Group labels for a splitter that reads them.
cv (int, splitter, iterable or None, default None) – As in
check_cv: None means five folds, stratified for a classifier.n_jobs (int or None, default None) – Only None or 1 is supported.
method (str, default ‘predict’) – Estimator method to call, for example
'predict_proba'.
- Returns:
predictions (Array or list) – Shape (n_samples,) or (n_samples, …) for numeric predictions: int64 when every fold returns 1-D integer predictions, float64 otherwise. Predictions that are not numbers (string labels) come back as a list.
Examples
>>> import numpy as np, mojolearn >>> from mojolearn.model_selection import cross_val_predict >>> rng = np.random.default_rng(0) >>> X = rng.random((1000, 8), dtype=np.float32) >>> y = rng.random(1000, dtype=np.float32) >>> pred = cross_val_predict(mojolearn.Ridge(alpha=1.0), X, y, cv=5)
- class mojolearn.model_selection.ParameterGrid(param_grid)[source]#
Bases:
objectGrid of parameter combinations, iterated as dicts.
Follows scikit-learn’s
ParameterGrid: every combination of each dict’s values, with keys in sorted order, and the dicts in list order. Supportslen()and indexing.- Parameters:
param_grid (dict or list of dicts) – Maps each parameter name to a nonempty list of values. Raises
TypeErrorfor a value that is not a list-like, andValueErrorfor an empty one.
Examples
>>> from mojolearn.model_selection import ParameterGrid >>> list(ParameterGrid({'alpha': [0.1, 1.0], 'fit_intercept': [True, False]}))[0] {'alpha': 0.1, 'fit_intercept': True}
- class mojolearn.model_selection.ParameterSampler(param_distributions, n_iter, *, random_state=None)[source]#
Bases:
objectRandom sample of parameter combinations, iterated as dicts.
Follows scikit-learn’s
ParameterSamplerfor lists of values: the combinations are sampled without replacement, as a random permutation of the full grid drawn by mojolearn’s counter-based random generator. When the grid has fewer thann_itercombinations, a warning is issued and every combination is returned.- Parameters:
param_distributions (dict or list of dicts) – Maps each parameter name to a list of values. scipy.stats distributions are not supported and raise
NotImplementedError.n_iter (int) – Number of combinations to sample.
random_state (int or None, default None) – Seed for the sampling.
Examples
>>> from mojolearn.model_selection import ParameterSampler >>> params = list(ParameterSampler({'alpha': [0.01, 0.1, 1.0, 10.0]}, ... n_iter=2, random_state=0))
- class mojolearn.model_selection.GridSearchCV(estimator, param_grid, *, scoring=None, n_jobs=None, refit=True, cv=None, verbose=0, pre_dispatch='2*n_jobs', error_score=nan, return_train_score=False)[source]#
Bases:
_BaseSearchExhaustive search over a parameter grid by cross-validation.
Follows scikit-learn’s
GridSearchCV. Every combination inParameterGrid(param_grid)is cross-validated on the same folds, one fit at a time. Withrefit, the best combination is then fit on all of X and y, andpredict,predict_proba,decision_function,transformandscorecall that fitted estimator.- Parameters:
estimator (estimator) – An unfitted estimator with
get_paramsandfit.param_grid (dict or list of dicts) – Parameter names mapped to lists of values.
scoring (str, callable, list, tuple, set, dict or None, default None) – As in
cross_validate. None uses the estimator’sscore.n_jobs (int or None, default None) – Only None or 1 is supported; other values raise
NotImplementedError.refit (bool, str or callable, default True) – Refit the best candidate on the whole data. With several metrics, the name of the metric that picks the best candidate, or a callable that takes
cv_results_and returns the best index.cv (int, splitter, iterable or None, default None) – As in
check_cv: None means five folds, stratified for a classifier.verbose (int, default 0) – Accepted for compatibility; nothing is printed.
pre_dispatch (str, default ‘2*n_jobs’) – Accepted for compatibility; not used.
error_score (‘raise’ or float, default nan) –
'raise'propagates a fit error; a number is recorded as the fold’s score instead.return_train_score (bool, default False) – Also record training scores (
mean_train_score).
- Variables:
cv_results_ (dict) –
params,param_<name>,split<i>_test_score,mean_test_score,std_test_scoreandrank_test_score(1 is best); with several metrics each score key ends in_<metric>.best_index_ (int) – Index of the best candidate in
cv_results_.best_score_ (float) – Mean cross-validated score of the best candidate (not set when
refitis a callable).best_params_ (dict) – Parameters of the best candidate.
best_estimator_ (estimator) – The best candidate refit on the whole data (only with
refit).scorer_ (callable or dict) – The scorer(s) used (only with
refit).n_splits_ (int) – Number of cross-validation splits.
multimetric_ (bool) – Whether several metrics were scored.
Examples
>>> import numpy as np, mojolearn >>> from mojolearn.model_selection import GridSearchCV >>> rng = np.random.default_rng(0) >>> X = rng.random((1000, 8), dtype=np.float32) >>> y = rng.random(1000, dtype=np.float32) >>> search = GridSearchCV(mojolearn.Ridge(), {'alpha': [0.1, 1.0, 10.0]}, cv=5) >>> search = search.fit(X, y) >>> best = search.best_params_
- class mojolearn.model_selection.RandomizedSearchCV(estimator, param_distributions, *, n_iter=10, scoring=None, n_jobs=None, refit=True, cv=None, verbose=0, pre_dispatch='2*n_jobs', random_state=None, error_score=nan, return_train_score=False)[source]#
Bases:
_BaseSearchRandomized search over parameter lists by cross-validation.
Follows scikit-learn’s
RandomizedSearchCVfor lists of values:n_itercombinations are sampled without replacement byParameterSamplerand each is cross-validated on the same folds, one fit at a time. scipy.stats distributions are not supported. Fitting, refitting, the result attributes and the prediction methods are as inGridSearchCV.- Parameters:
estimator (estimator) – An unfitted estimator with
get_paramsandfit.param_distributions (dict or list of dicts) – Parameter names mapped to lists of values.
n_iter (int, default 10) – Number of combinations to try.
scoring (str, callable, list, tuple, set, dict or None, default None) – As in
cross_validate.n_jobs (int or None, default None) – Only None or 1 is supported.
refit (bool, str or callable, default True) – As in
GridSearchCV.cv (int, splitter, iterable or None, default None) – As in
check_cv.verbose (int, default 0) – Accepted for compatibility; nothing is printed.
pre_dispatch (str, default ‘2*n_jobs’) – Accepted for compatibility; not used.
random_state (int or None, default None) – Seed for the sampling (mojolearn’s counter-based generator).
error_score (‘raise’ or float, default nan) – As in
GridSearchCV.return_train_score (bool, default False) – Also record training scores.
- Variables:
multimetric_ (cv_results_, best_index_, best_score_, best_params_, best_estimator_, scorer_, n_splits_,) – As in
GridSearchCV.
Examples
>>> import numpy as np, mojolearn >>> from mojolearn.model_selection import RandomizedSearchCV >>> rng = np.random.default_rng(0) >>> X = rng.random((1000, 8), dtype=np.float32) >>> y = rng.random(1000, dtype=np.float32) >>> search = RandomizedSearchCV(mojolearn.Ridge(), {'alpha': [0.01, 0.1, 1.0, 10.0]}, ... n_iter=2, random_state=0).fit(X, y)
- mojolearn.model_selection.validation_curve(estimator, X, y, *, param_name, param_range, groups=None, cv=None, scoring=None, n_jobs=None, error_score=nan)[source]#
Training and test scores for each value of one parameter.
Follows scikit-learn’s
validation_curve. For each value inparam_range, the estimator with that parameter set is cross-validated on the same folds, one fit at a time.- Parameters:
estimator (estimator) – An unfitted estimator with
get_params,set_paramsandfit.X (array-like of shape (n_samples, n_features)) – Training data.
y (array-like of shape (n_samples,)) – Targets.
param_name (str) – Name of the parameter to vary.
param_range (sequence) – Values of the parameter.
groups (array-like of shape (n_samples,), default None) – Group labels for a splitter that reads them.
cv (int, splitter, iterable or None, default None) – As in
check_cv.scoring (str, callable or None, default None) – As in
cross_val_score.n_jobs (int or None, default None) – Only None or 1 is supported.
error_score (‘raise’ or float, default nan) –
'raise'propagates a fit error; a number is recorded instead.
- Returns:
train_scores (Array of shape (len(param_range), n_splits), float64)
test_scores (Array of shape (len(param_range), n_splits), float64)
Examples
>>> import numpy as np, mojolearn >>> from mojolearn.model_selection import validation_curve >>> rng = np.random.default_rng(0) >>> X = rng.random((1000, 8), dtype=np.float32) >>> y = rng.random(1000, dtype=np.float32) >>> train, test = validation_curve(mojolearn.Ridge(), X, y, param_name='alpha', ... param_range=[0.1, 1.0, 10.0], cv=5)
- mojolearn.model_selection.learning_curve(estimator, X, y, *, groups=None, train_sizes=(0.1, 0.325, 0.55, 0.775, 1.0), cv=None, scoring=None, n_jobs=None, shuffle=False, random_state=None, error_score=nan, return_times=False)[source]#
Training and test scores for growing training-set sizes.
Follows scikit-learn’s
learning_curve, withoutexploit_incremental_learning. For each size and fold, a fresh clone is fit on the firstsizetraining rows of that fold and scored on those rows and on the fold’s test rows. Fits run one at a time.- Parameters:
estimator (estimator) – An unfitted estimator with
get_paramsandfit.X (array-like of shape (n_samples, n_features)) – Training data.
y (array-like of shape (n_samples,)) – Targets.
groups (array-like of shape (n_samples,), default None) – Group labels for a splitter that reads them.
train_sizes (sequence of float or int, default (0.1, 0.325, 0.55, 0.775, 1.0)) – Floats are fractions of the smallest training fold (rounded down), ints are row counts. Each must resolve to between 1 and the smallest training fold’s size, or
ValueErroris raised. Duplicates are dropped and the sizes sorted.cv (int, splitter, iterable or None, default None) – As in
check_cv.scoring (str, callable or None, default None) – As in
cross_val_score.n_jobs (int or None, default None) – Accepted for compatibility; fits always run serially.
shuffle (bool, default False) – Shuffle each fold’s training rows once before taking the prefixes.
random_state (int or None, default None) – Seed for the shuffle (mojolearn’s counter-based generator).
error_score (‘raise’ or float, default nan) – Accepted for compatibility; a fit error always propagates.
return_times (bool, default False) – Also return fit and score times.
- Returns:
train_sizes_abs (Array of shape (n_sizes,), int64) – The training-set sizes used.
train_scores (Array of shape (n_sizes, n_splits), float64)
test_scores (Array of shape (n_sizes, n_splits), float64)
fit_times (Array of shape (n_sizes, n_splits), float64) – Wall-clock seconds; only with
return_times=True.score_times (Array of shape (n_sizes, n_splits), float64) – Wall-clock seconds; only with
return_times=True.
Examples
>>> import numpy as np, mojolearn >>> from mojolearn.model_selection import learning_curve >>> rng = np.random.default_rng(0) >>> X = rng.random((1000, 8), dtype=np.float32) >>> y = rng.random(1000, dtype=np.float32) >>> sizes, train, test = learning_curve(mojolearn.Ridge(), X, y, cv=5)
- mojolearn.model_selection.permutation_test_score(estimator, X, y, *, groups=None, cv=None, n_permutations=100, n_jobs=None, random_state=0, verbose=0, scoring=None, fit_params=None, params=None)[source]#
Test the significance of a cross-validated score by permuting y.
Follows scikit-learn’s
permutation_test_score. The mean cross-validated score on the real targets is compared with the mean scores aftern_permutationsrandom permutations ofy(within each group whengroupsis given). The permutations come from mojolearn’s counter-based random generator.- Parameters:
estimator (estimator) – An unfitted estimator with
get_paramsandfit.X (array-like of shape (n_samples, n_features)) – Training data.
y (array-like of shape (n_samples,)) – Targets.
groups (array-like of shape (n_samples,), default None) – Permute
yonly within each group; also passed to the splitter.cv (int, splitter, iterable or None, default None) – As in
check_cv.n_permutations (int, default 100) – Number of permutations.
n_jobs (int or None, default None) – Accepted for compatibility; fits run serially.
random_state (int or None, default 0) – Seed for the permutations.
verbose (int, default 0) – Accepted for compatibility; nothing is printed.
scoring (str, callable or None, default None) – As in
cross_val_score.fit_params, params (dict or None, default None) – Accepted for compatibility; not passed to
fit.
- Returns:
score (float) – Mean cross-validated score on the real targets.
permutation_scores (Array of shape (n_permutations,), float64) – Mean score for each permutation.
pvalue (float) –
(C + 1) / (n_permutations + 1), whereCis the number of permutation scores greater than or equal toscore.
Examples
>>> import numpy as np, mojolearn >>> from mojolearn.model_selection import permutation_test_score >>> rng = np.random.default_rng(0) >>> X = rng.random((1000, 8), dtype=np.float32) >>> y = rng.random(1000, dtype=np.float32) >>> score, perm_scores, pvalue = permutation_test_score( ... mojolearn.Ridge(), X, y, cv=5, n_permutations=20)
- mojolearn.model_selection.get_scorer(scoring)[source]#
Return the scorer for a scoring argument.
Follows scikit-learn’s
get_scorer.- Parameters:
scoring (str, callable or None) – A name from
get_scorer_names(), a callable (returned as is), or None (returned as is, meaning the estimator’s ownscore).- Returns:
scorer (callable or None)
- Raises:
ValueError – If
scoringis an unknown name or not a str, callable or None.
Examples
>>> from mojolearn.model_selection import get_scorer >>> scorer = get_scorer('r2')
- mojolearn.model_selection.make_scorer(score_func, *, response_method='predict', greater_is_better=True, **kwargs)[source]#
Make a scorer from a metric function.
Follows scikit-learn’s
make_scorer. The scorer is called asscorer(estimator, X, y): it gets predictions from the estimator’s response method and returnsscore_func(y, predictions, **kwargs)as a float, negated whengreater_is_better=False.- Parameters:
score_func (callable) – Metric with signature
score_func(y_true, y_pred, **kwargs).response_method (str or tuple of str, default ‘predict’) – Estimator method that produces the predictions. A tuple is tried in order and the first method the estimator has is used.
greater_is_better (bool, default True) – False for a loss; the scorer then returns the negated value.
**kwargs – Extra keyword arguments passed to
score_func.
- Returns:
scorer (callable)
Examples
>>> from mojolearn import metrics >>> from mojolearn.model_selection import make_scorer >>> scorer = make_scorer(metrics.mean_absolute_error, greater_is_better=False)
- mojolearn.model_selection.get_scorer_names()[source]#
Return the sorted list of scorer names that
get_scoreraccepts.The names are scikit-learn’s, computed with
mojolearn.metrics. The multilabel'samples'averages (for example'f1_samples') are not available.Examples
>>> from mojolearn.model_selection import get_scorer_names >>> 'neg_mean_squared_error' in get_scorer_names() True