Skip to content

    Cross-Validation

    Machine Learning · Intermediate

    Definition

    K-fold cross-validation splits data into k parts, trains on k−1 and tests on the remaining one, k times.

    Gives a reliable estimate of how a model performs on unseen data.

    Diagram

    Fold1 testFold2 testFold3 testmean score

    Flowchart

    Shuffle dataSplit into k foldsTrain on k−1, test on 1Repeat k timesAverage scores

    Example: Average fold scores

    scores = [0.82, 0.85, 0.80, 0.88, 0.85]
    print(round(sum(scores) / len(scores), 3))

    Output: 0.84

    Complexity

    k times the training cost

    Key points

    • Stratified k-fold keeps class ratios
    • Time series needs forward-chaining splits

    Interview questions

    Why not one train/test split?

    A single split can be lucky or unlucky; k folds reduce variance.

    More Machine Learning topics