
A single train/test split gives one estimate of model performance. Cross-validation evaluates a model over multiple splits, giving a more informative view of how it performs on different subsets of data.
K-Fold cross-validation
With five folds, the dataset is divided into five groups. The model is trained five times. Each time, one group is held out for testing and the other four are used for training.
Every record appears in a test fold once.
Why StratifiedKFold?
For classification, especially imbalanced problems such as fraud detection, each fold should contain a similar proportion of each class.
StratifiedKFold aims to preserve class proportions:
cv = StratifiedKFold(
n_splits=5,
shuffle=True,
random_state=42,
)
Ordinary KFold does not deliberately preserve those proportions.
Track each fold in MLflow
A parent run can represent the overall cross-validation execution. Each fold can be logged as a nested child run:
with mlflow.start_run(run_name="cv-parent"):
for fold_idx in range(1, 6):
with mlflow.start_run(
run_name=f"fold-{fold_idx}",
nested=True,
):
mlflow.log_param("fold", fold_idx)
mlflow.log_metric("f1", 0.86)
The example metric is illustrative. In real code, calculate it from that fold's held-out predictions.
The child runs make individual fold results visible and comparable. The parent can store aggregate metrics.
Aggregate at the end
After all folds have completed, calculate the mean and standard deviation of each metric. The mean summarizes average performance; the standard deviation indicates how much results vary between folds.
Takeaway: cross-validation evaluates consistency across data splits, while nested MLflow runs preserve the detail of each fold under one parent execution.
Comments
Join the discussionNo comments yet. Be the first to comment.




