Introduction
In our previous article, we explored the complete Machine Learning workflow, from collecting data to deploying a trained model.
Today, let's understand one of the most important concepts in Machine Learning:
Why do we divide a dataset into Training, Validation, and Testing sets?
Imagine teaching a student for an examination. The student first studies using textbooks, practices questions to improve performance, and finally takes an examination with questions they haven't seen before.
Machine Learning models follow a similar approach!
Let's understand how it works.
1. Training Data – Learning from Examples
Training data is the portion of a dataset used to teach a Machine Learning model.
During training, the model identifies patterns, relationships, and connections between input features and expected outputs.
Example: House Price Prediction
Suppose we have a dataset containing information about 1,000 houses.
The model learns from details such as:
By analyzing these examples, the model learns how different features influence house prices.
Purpose: Help the model learn patterns from available data.
2. Validation Data – Improving the Model
After training, how do we decide whether our model needs improvement?
This is where validation data comes in.
Validation data is used to evaluate the model during development and help make decisions about its configuration.
It helps us:
- Compare different algorithms.
- Select the best-performing model.
For example, we might experiment with different settings of a Decision Tree algorithm and use validation performance to choose an appropriate configuration.
Purpose: Help improve and select the model before final testing.
3. Testing Data – Evaluating Final Performance
Testing data is used to evaluate the final model on data it has not previously used during training or model selection.
It provides an estimate of how well the model may perform on new, unseen data.
For example, after training and tuning our house price prediction model, we evaluate it using the testing dataset.
If the model performs well on this unseen data, it suggests that it has learned useful patterns rather than simply memorizing training examples.
Purpose: Estimate how well the final model generalizes to unseen data.
4. Understanding the Difference
Let's compare all three datasets.
Feature | Training | Validation | Testing |
|---|
Main purpose | Learning | Model tuning | Final evaluation |
Used during training? | Yes | No | No |
Hyperparameter tuning? | No | Yes | No |
Frequency of use | Repeatedly | During development | Final evaluation |
Data | Training examples | Validation examples | Unseen examples |
5. How Much Data Should We Allocate?
One common approach is to divide a dataset into three parts.
A commonly used split is:
Example:
Suppose we have 10,000 records.
Dataset | Percentage | Records |
|---|
Training | 70% | 7,000 |
Validation | 15% | 1,500 |
Testing | 15% | 1,500 |
Total | 100% | 10,000 |
However, this is not a universal rule. Other splits, such as 80:10:10 or 80:20, are also common.
The ideal split depends on dataset size, problem type, and available data.
For time-series problems, chronological splitting is often more appropriate than random splitting.
6. What Happens If We Don't Split Data Properly?
Two important problems can occur.
Overfitting
Overfitting happens when a model learns the training data too closely, including noise and unnecessary details.
As a result:
- Training performance may be very high.
- Performance on unseen data may be poor.
Data Leakage
Data leakage occurs when information from outside the training process improperly influences model training or selection.
For example, if we use testing data repeatedly to tune our model, the test results are no longer an independent estimate of final performance.
This is why keeping the testing dataset separate is extremely important.
7. Practical Example Using Python
Let's see how we can divide a dataset using Python's Scikit-learn library.
from sklearn.model_selection import train_test_split# Divide features and targetX = df.drop("price", axis=1)y = df["price"]# First split: 70% training, 30% remainingX_train, X_temp, y_train, y_temp = train_test_split( X, y, test_size=0.30, random_state=42)# Second split: 15% validation, 15% testingX_val, X_test, y_val, y_test = train_test_split( X_temp, y_temp, test_size=0.50, random_state=42)print("Training:", len(X_train))print("Validation:", len(X_val))print("Testing:", len(X_test))
Here:
-
X contains input features.
-
y contains target values.
-
random_state=42 ensures reproducible splitting.
- The final dataset is divided into 70%, 15%, and 15%.
Note: This example assumes df is an already loaded and cleaned Pandas DataFrame.
Conclusion
Training, validation, and testing datasets are fundamental to building reliable Machine Learning models.
Remember these three simple points:
- Training: The model learns.
- Validation: We improve and select the model.
- Testing: We evaluate its final performance.
A model that performs well on training data is not necessarily a good model. What really matters is how well it performs on data it has never encountered before.
Understanding this concept will help us build Machine Learning systems that are more reliable and capable of handling real-world problems.
Coming Next – Day 5: Understanding Overfitting and Underfitting in Machine Learning.
We'll explore why models sometimes fail to generalize, how to identify these problems, and techniques to overcome them.
Loading article