cross_validation¶
Scikit-learn-compatible cross-validation data classes.
Classes:
| Name | Description |
|---|---|
KFoldStratified | Stratify continuous targets across K-fold cross-validation. |
Methods:
| Name | Description |
|---|---|
resolve_cv | Resolve 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:
| Name | Type | Description | Default |
|---|---|---|---|
n_splits | Number of folds. Must be at least 2. Defaults to 3. | 3 | |
shuffle | Whether to shuffle the data before splitting into batches. | False | |
random_state | Pseudo-random number generator state used for random sampling. If None, use the default numpy RNG for shuffling. | None |
Methods:
| Name | Description |
|---|---|
split | Generate 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:
| Name | Type | Description | Default |
|---|---|---|---|
X | Training 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 | |
y | The target variable of shape (n_samples,) for supervised learning problems. Stratification is done based on the y labels. | None | |
groups | Always ignored, exists for compatibility. | None |
Returns:
| Name | Type | Description |
|---|---|---|
train | The training set indices for that split (ndarray). | |
test | The 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:
| Name | Type | Description | Default |
|---|---|---|---|
cv | 'loo' (LeaveOneOut), 'logo' (LeaveOneGroupOut — pass the grouping variable via groups), an int fold count, or an sklearn splitter (returned unchanged). | required | |
groups | Group labels, or None. Only consulted for int specs. | None | |
classifier | bool | Whether the downstream model is a classifier — an int spec becomes the stratified variant (StratifiedKFold, or StratifiedGroupKFold with groups) for classifiers. | False |
shuffle | bool | Whether an int spec’s KFold variant shuffles samples before splitting. Ignored for the group variants (fold membership is set by groups). | False |
random_state | int | None | Seed for shuffle. | None |
Returns:
| Type | Description |
|---|---|
| An sklearn splitter instance. |