
Dan Patiño
AI Strategy & Innovation at Coderhouse
Artificial Intelligence
Cross-validation is a method for estimating how well a machine learning model will perform on data it has not seen yet. Instead of training once and trusting a single test score, you repeatedly split the data into training and validation folds, fit the model on each training fold, and score it on the held-out fold. The average score is a more honest estimate of generalization than one lucky split.
Why it matters now: teams ship models into products, ranking systems, and AI features where a glowing training metric can hide overfitting—strong performance on familiar data that collapses on new users or new weeks. Cross-validation is one of the simplest defenses against that failure mode.
Why a single train/test split is not enough
A common workflow is to split data into a training set and a test set, tune the model on training data, and report the test score. That works for a first baseline, but it has two problems. First, the test score depends on which rows landed in the test set; another split can look very different. Second, if you keep tuning until the test score improves, the test set stops being unseen—you have overfit to it.
scikit-learn’s cross-validation documentation explains this clearly: you need a validation procedure for model selection, and a truly held-out test set (or an outer loop) for a final estimate. Cross-validation is the standard way to reuse limited labeled data for that validation step.
How k-fold cross-validation works
In k-fold cross-validation you divide the dataset into k roughly equal parts (folds). For each fold i:
Train on the other k − 1 folds.
Evaluate on fold i.
Store the score (accuracy, F1, RMSE, and so on).
Then you average the k scores (and often look at their spread). Typical choices are k = 5 or k = 10. Leave-one-out is the extreme case where each fold is a single sample; it is expensive and not always better.
This process answers: “If I train this recipe on most of my data, how does it behave on the rest?” It does not invent new data, but it squeezes more signal from the sample you have.
Variants you will see in real projects
Stratified k-fold
For classification with imbalanced labels, stratified folds keep class proportions similar in every fold. That prevents a fold from accidentally containing almost no positives, which would make precision and recall noisy. Metrics like those in a confusion matrix only make sense when each fold sees a fair mix of classes.
Group and time-aware splits
If many rows belong to the same user, device, or store, random folds can leak information: the model sees a user in training and again in validation. Group cross-validation keeps whole groups together. For time series, you must respect order—future data cannot train a model that predicts the past—so rolling or expanding time splits replace shuffled folds.
Nested cross-validation
When you also tune hyperparameters, an inner CV loop selects the best settings and an outer loop estimates performance. That avoids optimistic scores from tuning and evaluating on the same folds.
Cross-validation and overfitting
Cross-validation does not magically remove overfitting, but it helps you detect it. If training scores are high and CV scores are much lower, the model is memorizing noise. scikit-learn’s classic underfitting vs overfitting example shows how model complexity interacts with train and validation error: too simple underfits both; too complex fits training data and fails on held-out folds.
Use CV to compare algorithms and hyperparameters, then confirm the winner on a final held-out test set you never touched during selection. For the broader learning setup—labeled examples versus discovery without labels—see supervised vs unsupervised learning.
Practical tips for analysts and ML practitioners
Match the split to the deployment. Random folds for i.i.d. tabular rows; groups for users; time splits for forecasts.
Report mean and variation. A mean F1 of 0.82 with folds ranging from 0.70 to 0.90 is different from a tight 0.81–0.83.
Keep preprocessing inside the fold. Fit scalers and encoders on the training fold only, then apply them to the validation fold, or you leak information.
Do not cherry-pick the best fold. The point is the average (and distribution), not the luckiest split.
FAQ
Is cross-validation only for supervised learning?
It is most common for supervised tasks with a clear score, but similar hold-out ideas appear in unsupervised evaluation (for example, reconstructing held-out data) and in tuning retrieval or ranking systems.
How many folds should I use?
Five or ten folds are a solid default. More folds give each train set more data but cost more compute and can increase variance of the estimate. For very small datasets, leave-one-out or repeated CV can help; for huge datasets, a single large validation set may be enough.
Does a good CV score guarantee production success?
No. Distribution shift, delayed labels, and feedback loops can still hurt. CV estimates performance under the assumption that future data resembles your folds. Monitor live metrics after launch.
Learn to build and evaluate models with live practice
If you want hands-on practice with data, models, and evaluation, guided by practitioners, Coderhouse offers live online courses with one class per week:
AI Engineering Course: 8 weeks for experienced developers moving into AI engineering, covering LLM applications, retrieval, agents, evaluation, and deployment.
Data Analytics Course: 11 weeks for career switchers, analysts, and data-driven professionals, covering metrics, SQL, Python, visualization, experimentation, and AI-assisted analysis.
I'm Dan Patiño, head of AI Strategy & Innovation at Coderhouse. My day-to-day work involves merging the tactical management of e-commerce (CRO, Email Marketing and SEO) with the development of disruptive solutions. I specialize in building internal AI-powered apps to automate tasks and boost innovation within the team. I firmly believe that technology is strategy's best ally. To dive deeper into my professional journey, I'll be waiting for you on my LinkedIn profile.