Supervised Learning with scikit-learn
Hands-on 4-hour DataCamp course teaching classification, regression, tuning, and pipelines in scikit-learn with real datasets in Python.
Machine learning data is split so that a model is judged on data it has never seen. The training set is used to fit the model, the validation set to compare models and tune settings, and the test set only once, at the end, to estimate real-world performance. Cross-validation rotates the validation role across several folds to get a more reliable estimate from limited data.
A model that scores 99% on the data it learned from tells you almost nothing. It may simply have memorized that data, which is overfitting. The whole point of splitting data is to simulate the future: the model will meet new customers, new transactions and new images, so it must be tested on examples it hasn't seen.
If you evaluate a model on its training data, you will ship models that look excellent in a notebook and disappoint in production. Proper splitting is how a data scientist can honestly say, "We expect around 82% accuracy on new customers," and be roughly right. It's also one of the first things a reviewer checks in any ML project, interview take-home or ML engineering code review.
| Set | Purpose | Used when | Typical share |
|---|---|---|---|
| Training | Fit the model's parameters | Every time a model is trained | 60–80% |
| Validation | Compare models, tune hyperparameters, choose a threshold | Repeatedly during development | 10–20% |
| Test | Final, unbiased estimate of performance | Once, at the very end | 10–20% |
Think of it like exam preparation. Training data is the textbook. Validation data is the practice exams you use to adjust how you study. The test set is the real exam: if you peek at it while studying, your score no longer measures what you know.
A bank has 10,000 historical loan applications and wants to predict default.
Step 1: Split once, up front. Using a 70/15/15 split:
Because defaults are rare (say 8% of loans), use a stratified split so each set keeps roughly 8% defaults. Otherwise one set could end up with very few positive cases.
Step 2: Develop using training and validation. Train three candidate models on the 7,000 rows and score them on the 1,500 validation rows:
| Model | Validation recall on defaults |
|---|---|
| Logistic regression | 0.62 |
| Random forest | 0.71 |
| Gradient boosting | 0.74 |
You tune the gradient boosting model's settings and choose a decision threshold, still only looking at validation results. Recall is the right lens here because missing a default is costly; see Precision vs Recall.
Step 3: Test once. Retrain the chosen model, typically on training plus validation data, and score it on the 1,500 untouched test rows. If test recall is 0.72, that is your honest estimate. If it is far below validation (say 0.55), you have probably overfitted to the validation set through too much tuning.
With a small dataset, a single validation split is noisy: a lucky or unlucky 1,500 rows can swing the result. k-fold cross-validation solves this.
Example. Five-fold cross-validation gives accuracy scores of 0.81, 0.79, 0.83, 0.80 and 0.82. The mean is 0.81 and the standard deviation is about 0.016. A small spread means the estimate is stable. A spread of ±0.10 would warn you that performance depends heavily on which data the model sees.
| Single validation split | k-fold cross-validation | |
|---|---|---|
| Compute cost | Low: train once per setting | k times higher |
| Reliability | Noisy on small data | More stable estimate |
| Best for | Large datasets, deep learning | Small to medium tabular data |
Variants worth knowing:
Pipeline.Expect questions such as: "Why do we need a separate test set if we already have a validation set?", "What is data leakage? Give an example." and "How would you validate a forecasting model?" Good answers mention tuning bias, preprocessing leakage and time-based splits. Take-home assignments are also routinely judged on whether the candidate split the data correctly before doing anything else.
Supervised Learning with scikit-learn (DataCamp) covers train-test splits, cross-validation and hyperparameter tuning with hands-on exercises, and Machine Learning with Python (IBM) includes a full module on evaluation and validation. Then revisit Overfitting vs Underfitting and Classification vs Regression. Browse all Machine Learning courses.
What is a good train/test split ratio? 70/15/15 or 80/10/10 are common starting points. With very large datasets you can give a smaller share to validation and test, because even 1% of ten million rows is plenty.
Do I always need a validation set? You need some way to choose between models without touching the test set. That can be a separate validation set or cross-validation on the training data.
What value of k should I use? Five or ten folds are standard. More folds use more data for training each time but cost more compute.
Is cross-validation used in deep learning? Less often. Deep learning datasets are usually large and training is expensive, so a single train/validation/test split is the norm.
Train on one part of the data, make decisions with a second and judge the final model on a third that you touch only once. Use cross-validation when data is limited, stratify when classes are imbalanced, split by time for time series and keep preprocessing inside the training folds. Get this right and your reported accuracy will mean something.