What is cross-validation?
Cross-validation is a technique for evaluating machine learning models in which the data is split into parts and the model is trained and tested several times, rotating which part is held out. Instead of a single score, you get an average performance across runs, which reduces the chance that your conclusion depends on one lucky split. The k tells you how many parts, or folds, the data is cut into.
What is cross-validation, in plain terms?
Cross-validation is the process of splitting the data into parts, training the model several times, and testing each version on a different part. The final result is not a single number but an average, reported together with the variation between runs.
The problem it solves is straightforward. If you split the data once into train and test, the measured performance depends on which rows landed on each side. A lucky split inflates the score; an unlucky one drags it down. Cross-validation repeats the experiment and shows whether the performance holds up.
In practice, it is used to compare algorithms, tune hyperparameters, and estimate how a model should behave on data it has not seen. It is an evaluation step, not a type of model.
- Shuffle the data when order does not matter.
- Split it into k parts of similar size.
- Train on k−1 parts and test on the part left out.
- Repeat until every part has served as the test set once.
- Summarize the result as an average and a spread across folds.
What does k-fold mean, and how does k change the result?
The k is the number of folds, meaning how many parts the data is cut into. With k = 5, the model is trained five times, always on 80% of the data, and tested on the remaining 20%. With k = 10, there are ten training runs, each on 90% of the data.
A larger k means more training runs and therefore more time. In exchange, each model sees more training data and the estimate tends to be less sensitive to the specific split. A smaller k is faster and is the better choice when the model is expensive to train or the dataset is large.
There is an extreme case: leave-one-out, where each observation is held out once. The number of training runs then equals the number of rows in the dataset, which is usually prohibitive outside of small datasets.
Values such as 5 and 10 are common conventions, not rules. What matters is that the k you choose fits your processing time and that the result is reported with the variation across folds, not just the average.
Why does cross-validation take more computing time?
The computing cost of cross-validation is roughly k times the cost of training the model once. If one training run takes a certain amount of time, k folds take k times that, plus the cost of the evaluations.
That multiplier grows along with three things: the value of k, the size of the dataset, and the complexity of the model. Algorithms that are already slow to train, such as Gradient Boosting, XGBoost, and LightGBM with many trees, feel the effect most directly.
When cross-validation is combined with hyperparameter search, the cost multiplies again: every parameter combination goes through the entire fold cycle. It is common for grid search to dominate the total time of an experiment.
Ways to keep the cost under control include lowering k, reserving cross-validation for the final comparison step, limiting the hyperparameter search space, or working with a sample of the dataset during exploration.
When is cross-validation worth it instead of a single split?
It is worth it when the dataset is small or moderate and you need a reliable estimate. It is also worth it when the decision matters: choosing between Random Forest and Gradient Boosting on a single split can lead to the wrong conclusion.
It is worth less when the dataset is very large, because a single split already provides a robust test set, or when each training run is too expensive. In those cases, one well-made split does the job.
There is also the case of data with a time order. In time series, shuffling the folds lets the model train on the future to predict the past, which inflates the score. There, the split has to respect the order of time.
Where does cross-validation fit in the Python for Data Science and Analytics course?
Cross-validation is part of the machine learning module of the Python for Data Science and Analytics course, from zero to advanced. That module covers linear and logistic regression, K-Means, SVM, Naive Bayes, decision trees, Random Forest, Gradient Boosting, XGBoost, LightGBM, and feature selection, with cross-validation as the evaluation step that ties them together.
The course is organized into three main modules — Basic, Intermediate, and Advanced — plus bonus modules. Before machine learning, it covers IDE and Jupyter Notebook setup, basic functions, NumPy and Pandas, user-defined functions, loops and conditionals, data visualization, data cleaning and preparation, and outlier detection.
It closes with a final project: exploratory analysis and end-to-end machine learning. That is where evaluation choices such as the number of folds stop being theory and become part of a decision you have to justify.
Lessons are recorded in high quality and watched at your own pace, with guided practice exercises and real market projects to build a portfolio. Access is unlimited, with no expiration date, and completion comes with a certificate. There is also an exclusive community for networking. No prior programming experience is required to start.
Enrollment is R$ 470, or 12 payments of R$ 20.37 on a credit card, through the Hotmart checkout, which also accepts boleto. The course site is python.paanalytics.net, and the checkout page is where you complete enrollment.
| k value | Training runs | Data used per training run | Training cost vs. a single run |
|---|---|---|---|
| 5 | 5 | 80% | About 5x |
| 10 | 10 | 90% | About 10x |
| Leave-one-out (n rows) | n | (n−1)/n | About n× |
Figures derived from the definition of k-fold cross-validation. They are arithmetic, not measurements of the course or of any specific model.
Learn cross-validation hands-on, with Python
Secure payment via Hotmart, up to 12 installments.
Enroll now Product siteFrequently asked questions
Is cross-validation the same as a train-test split?
No. A train-test split holds out one portion of the data and produces one score. Cross-validation repeats the split k times, rotating which part is held out, and reports an average with the variation between folds. The single split is faster; cross-validation gives a more stable estimate.
Which value of k should I use?
There is no universal answer. 5 and 10 are common conventions because they balance training time against the amount of data each model sees. Choose a k that fits your processing time and report the spread across folds, not only the average.
Does cross-validation prevent overfitting?
It does not prevent it, but it makes overfitting easier to detect. A model that scores well on one fold and poorly on the others is showing unstable performance. The variation between folds is the signal, which is why reporting only the average hides useful information.
Can cross-validation be used on time series data?
Not with shuffled folds. If the data has a time order, shuffling lets the model train on later observations to predict earlier ones, which inflates the score. The split has to respect the order of time so that training always comes before testing.
Do I need programming experience to follow the cross-validation module?
No. The course starts from zero and does not require prior programming knowledge. It builds up from IDE and Jupyter Notebook setup, basic functions, NumPy and Pandas, user-defined functions, loops and conditionals, and data preparation before reaching machine learning and cross-validation.