Contents
Cross-Validation Explained
Cross-validation is a technique that tests a model's performance more reliably by splitting the data into multiple parts and training and testing the model several times, instead of just once.
Why cross-validation matters
- A single train-test split can give misleading results by chance.
- It gives a more accurate, stable measure of model performance.
- It makes better use of limited data, since every part gets used for both training and testing.
K-Fold Cross Validation
The dataset is divided into K equal parts, called folds. The model trains on K minus 1 folds and tests on the remaining one, repeating this process until every fold has been used for testing.
from sklearn.model_selection import cross_val_score
from sklearn.linear_model import LogisticRegression
model = LogisticRegression()
scores = cross_val_score(model, X, y, cv=5)
print(scores)
print(scores.mean())cv=5 means the data is split into 5 folds, so the model is trained and tested 5 times. scores.mean() gives the average accuracy across all folds, which is a more trustworthy measure than a single test result.
Cross-validation helps confirm that a model's performance is consistent, and not just a result of a lucky data split.
Machine Learning Tutorial
Abhishek Pati








