Skip to article frontmatterSkip to article content
Site not loading correctly?

This may be due to an incorrect BASE_URL configuration. See the MyST Documentation for reference.

cross_validation

cross_validation

Scikit-learn-compatible cross-validation data classes.

Classes:

NameDescription
KFoldStratifiedStratify continuous targets across K-fold cross-validation.

Methods:

NameDescription
resolve_cvResolve a cv spec (int, sklearn-style name, or splitter) into an sklearn splitter.

Classes

KFoldStratified

KFoldStratified(n_splits = 3, *, shuffle = False, random_state = None)

Bases: _BaseKFold

Stratify continuous targets across K-fold cross-validation.

Unlike the scikit-learn equivalent, this iterator stratifies continuous data.

Provides train/test indices to split data in train test sets. Samples are ordered by their continuous target y and dealt round-robin into k folds so each fold spans the full range of y. Each fold is then used as a validation set once while the k - 1 remaining folds form the training set.

Parameters:

NameTypeDescriptionDefault
n_splitsNumber of folds. Must be at least 2. Defaults to 3.3
shuffleWhether to shuffle the data before splitting into batches.False
random_statePseudo-random number generator state used for random sampling. If None, use the default numpy RNG for shuffling.None

Methods:

NameDescription
splitGenerate indices to split data into training and test set.

Methods

split
split(X, y = None, groups = None)

Generate indices to split data into training and test set.

Parameters:

NameTypeDescriptionDefault
XTraining data of shape (n_samples, n_features), where n_samples is the number of samples and n_features is the number of features. Note that providing y is sufficient to generate the splits, hence np.zeros(n_samples) may be used as a placeholder for X instead of actual training data.required
yThe target variable of shape (n_samples,) for supervised learning problems. Stratification is done based on the y labels.None
groupsAlways ignored, exists for compatibility.None

Returns:

NameTypeDescription
trainThe training set indices for that split (ndarray).
testThe testing set indices for that split (ndarray).

Methods

resolve_cv

resolve_cv(cv, *, groups = None, classifier: bool = False, shuffle: bool = False, random_state: int | None = None)

Resolve a cv spec (int, sklearn-style name, or splitter) into an sklearn splitter.

The single cv-resolution rule shared by BrainData.predict, BrainCollection.predict, and BrainCollection.predict_group. String names follow sklearn’s splitter classes; an int spec must honor groups when one is supplied — plain KFold silently ignores its groups argument, which previously produced folds byte-identical to passing no groups at all.

Parameters:

NameTypeDescriptionDefault
cv'loo' (LeaveOneOut), 'logo' (LeaveOneGroupOut — pass the grouping variable via groups), an int fold count, or an sklearn splitter (returned unchanged).required
groupsGroup labels, or None. Only consulted for int specs.None
classifierboolWhether the downstream model is a classifier — an int spec becomes the stratified variant (StratifiedKFold, or StratifiedGroupKFold with groups) for classifiers.False
shuffleboolWhether an int spec’s KFold variant shuffles samples before splitting. Ignored for the group variants (fold membership is set by groups).False
random_stateint | NoneSeed for shuffle.None

Returns:

TypeDescription
An sklearn splitter instance.