All articles
Machine Learning
ml-evaluation

Mastering Data Splitting: Train, Validation, and Test Sets in Machine Learning

Learn the essential techniques for splitting your machine learning data to ensure robust model generalisation and avoid overfitting.

Math Instructor AI 22 September 2026 8 min read

Mastering Data Splitting: Train, Validation, and Test Sets

In machine learning, the primary goal is to build models that perform well on unseen data. If you evaluate your model on the same data used for training, you are measuring memorisation rather than generalisation. This article explores the standard practice of splitting your dataset into training, validation, and test sets, a fundamental skill for any undergraduate student aiming to build reliable predictive models.

By the end of this guide, you will understand why these splits are necessary, how to allocate your data effectively, and how to avoid common pitfalls that lead to biased performance metrics. Mastering these concepts is essential for your coursework and for developing professional-grade machine learning pipelines.

The Purpose of the Three-Way Split

To build a robust model, we divide our data into three distinct roles:

  1. Training Set: Used to fit the model parameters. The model learns patterns, weights, and biases from this data.
  2. Validation Set: Used for hyperparameter tuning and model selection. It acts as a proxy for unseen data, allowing you to compare different model architectures or settings without touching the final test set.
  3. Test Set: Used exactly once at the very end of the project to provide an unbiased estimate of the final model's performance.

If you use the test set to tune your model, you introduce 'data leakage', where the model indirectly learns from the test data, leading to overly optimistic performance results.

Worked Example 1: Simple Random Splitting

Imagine you have a dataset of $N = 1000$ samples. A common heuristic for a medium-sized dataset is a 60/20/20 split.

Step 1: Calculate sizes

  • Training: $1000 \times 0.6 = 600$ samples
  • Validation: $1000 \times 0.2 = 200$ samples
  • Test: $1000 \times 0.2 = 200$ samples

Step 2: Implementation Using Python's scikit-learn, you would first split the data into training and a temporary set, then split the temporary set into validation and test sets:

from sklearn.model_selection import train_test_split
# First split: 60% train, 40% temp
X_train, X_temp, y_train, y_temp = train_test_split(X, y, test_size=0.4)
# Second split: 20% validation, 20% test (half of the 40% temp)
X_val, X_test, y_val, y_test = train_test_split(X_temp, y_temp, test_size=0.5)

Stratification: Ensuring Representative Splits

In classification tasks, your dataset might be imbalanced. If you perform a simple random split, your test set might end up with zero instances of a minority class. Stratification ensures that the proportion of classes in each split matches the original dataset.

Worked Example 2: Stratified Splitting Suppose you have 100 samples: 90 'Class A' and 10 'Class B'.

  • Without stratification, a random split might put all 10 'Class B' samples into the training set.
  • With stratification, the split ensures that each subset maintains the 9:1 ratio.

In scikit-learn, you achieve this by setting the stratify parameter: train_test_split(X, y, test_size=0.2, stratify=y)

Handling Temporal Data

If your data is a time series, random shuffling is strictly forbidden. Shuffling destroys the chronological order, allowing the model to 'look into the future' during training. Instead, you must use a chronological split where the training set consists of earlier data and the test set consists of the most recent data.

Common Mistakes

  1. Data Leakage: Performing preprocessing (like scaling or imputation) on the entire dataset before splitting. Always fit your scaler on the training set only, then apply it to the validation and test sets.
  2. Over-tuning on Validation: Using the validation set to make too many decisions can lead to overfitting on the validation set itself. If you have enough data, consider cross-validation.
  3. Ignoring Class Imbalance: Failing to use stratified splits in classification tasks, leading to unreliable evaluation metrics.
  4. Touching the Test Set Early: Using the test set to compare models. The test set must remain 'locked' until the final evaluation.

Frequently Asked Questions

Q: Why not just use a train/test split? A: A validation set is required for hyperparameter tuning. If you only have a test set, you have no way to tune your model without biasing your final evaluation.

Q: How much data should I put in each set? A: For small datasets, use cross-validation. For medium datasets, 60/20/20 is standard. For very large datasets, you can afford a smaller percentage for validation and testing (e.g., 98/1/1).

Q: Can I reuse the test set? A: No. The test set should be used exactly once to report final performance. Reusing it turns it into a de facto validation set.

Conclusion

Proper data splitting is the cornerstone of honest machine learning. By separating your data into training, validation, and test sets, you ensure that your model's performance metrics reflect its true ability to generalise. To see these concepts in action with interactive visualisations, head over to MathInstructor AI and generate a free animated lesson on data splitting today.

Topics

machine learning
data splitting
train test split
validation set
ml-evaluation
holdout
overfitting
generalisation
hyperparameter tuning
data leakage

Want this explained out loud?

Turn any question into a narrated, animated lesson in seconds.

Try the Studio free