KFold
Module: strata.model_selection • Kind: struct • Traits: Movable
Source: strata/model_selection/kfold.mojo
struct KFold(Movable)from strata.model_selection import KFoldK-Fold cross-validator.
Provides train/validation indices to split data into consecutive or shuffled folds. Each fold is used once as a validation set while the remaining folds form the training set.
Constructors
Section titled “Constructors”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. |
Methods Overview
Section titled “Methods Overview”| 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. |
Method Details
Section titled “Method Details”KFold.get_n_splits()
Section titled “KFold.get_n_splits()”def get_n_splits(self) -> IntReturns the number of splitting iterations in the cross-validator.
Returns: Int
KFold.split()
Section titled “KFold.split()”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.”Example
Section titled “Example”from strata.model_selection import KFold
var kf = KFold(n_splits=5, shuffle=True, random_state=42)var folds = kf.split(n_samples=100)