Skip to content

KFold

Module: strata.model_selection • Kind: struct • Traits: Movable
Source: strata/model_selection/kfold.mojo

struct KFold(Movable)
from strata.model_selection import KFold

K-Fold cross-validator.

Provides train/validation indices to split data into KK consecutive or shuffled folds. Each fold is used once as a validation set while the remaining K−1K-1 folds form the training set.


def __init__(
out self,
n_splits: Int = 5,
shuffle: Bool = False,
random_state: Int = 42,
)

Initialize the KFold splitter.

Argument Type Description
n_splits Int Number of folds (must be at least 2). Default 5.
shuffle Bool Whether to shuffle the data before splitting. Default False.
random_state Int PRNG seed when shuffle is True. Default 42.

Method Description
KFold.get_n_splits() Returns the number of splitting iterations in the cross-validator.
KFold.split() Generates indices to split data into training and test sets.

def get_n_splits(self) -> Int

Returns the number of splitting iterations in the cross-validator.

Returns: Int


def split(self, n_samples: Int) -> List[Split]
def split[dtype: DType](self, X: Matrix[dtype]) -> List[Split]

Overload Note: This method supports multiple overloaded call signatures.

Generates indices to split data into training and test sets.

Parameter Type Description
n_samples Int Total number of samples in the dataset.
X Matrix[dtype] Feature matrix.

Returns: List[Split] — List of Split objects containing train and validation indices.

Section titled “Returns: List[Split] — List of Split objects containing train and validation indices.”
from strata.model_selection import KFold
var kf = KFold(n_splits=5, shuffle=True, random_state=42)
var folds = kf.split(n_samples=100)