Skip to content

nltools.cross_validation

The cross-validation splitter nltools adds to the scikit-learn ones.

Scikit-learn-compatible cross-validation data classes.

Classes:

Name Description
KFoldStratified

Stratify continuous targets across K-fold cross-validation.

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 int

Number of folds. Must be at least 2. Defaults to 3.

3
shuffle bool

Whether to break ties in y randomly before dealing samples into folds. Default False.

False
random_state int | RandomState

Seed or RandomState for the tie-break shuffle. If None, use the default numpy RNG.

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 array - like

Training data of shape (n_samples, n_features). Only y is needed to generate the splits, so np.zeros(n_samples) works as a placeholder.

required
y array - like

Continuous target of shape (n_samples,) or (n_samples, 1); stratification is based on its ordering.

None
groups array - like

Always ignored; exists for sklearn compatibility.

None

Yields:

Type Description
tuple[ndarray, ndarray]

(train, test) — the training set indices and the testing set indices for that split.

Raises:

Type Description
ValueError

If y carries more than one target per sample. Ordering samples by a multi-target response has no single meaning, and flattening one would produce more fold labels than there are rows.