A robust statistical method for evaluating machine learning models by partitioning the data into multiple subsets, training the model on some subsets, and validating it on the remaining ones, rotating until every subset has been used for validation.
Instead of taking one single practice test to see if you’re ready for the final, you take 5 different practice tests, each covering a different part of the material. This gives you a much more accurate idea of what you actually know.
In K-Fold Cross-Validation, the training data is split into ‘K’ equal folds. The model is trained K times; each time, K-1 folds are used for training, and the remaining 1 fold is used for validation. The final performance metric is the average of all K runs. This drastically reduces the variance of the performance estimate compared to a single train/validation split.
A chef testing a new recipe. Instead of having just one friend taste it (single split), they cook the recipe 5 times, slightly adjusting the ingredients, and have 5 different friends taste it. The average feedback is a much truer measure of the recipe’s quality.
# Conceptual: K-Fold Cross-Validation using scikit-learn
from sklearn.model_selection import KFold, cross_val_score
from sklearn.ensemble import RandomForestClassifier
# X = features, y = labels
model = RandomForestClassifier()
# Set up 5-fold cross-validation
kfold = KFold(n_splits=5, shuffle=True, random_state=42)
# Evaluate model
scores = cross_val_score(model, X, y, cv=kfold, scoring='accuracy')
print(f"Accuracy for each fold: {scores}")
print(f"Mean accuracy: {scores.mean():.3f} (+/- {scores.std():.3f})")