Test Data
Test data is a separate set of examples used for the final evaluation of a Machine Learning model after the model development process is complete.
Test data gives us a final check using examples the model has not used during development.
The test set should remain separate from training and model-selection decisions so that the final evaluation is as fair as possible.
What Is Test Data?
Test data is a portion of the original dataset that is kept aside and not used to train the model.
It is also not normally used to repeatedly make development decisions.
Instead, we use it near the end to estimate how well the final model performs on unseen examples.
Training, Validation, and Test
The model learns patterns from these examples.
Used during development to compare choices.
Used for the final evaluation.
Why Keep the Test Data Separate?
Imagine you build a model and keep checking its performance against the test set.
Every time the test result influences a change to your model, you are indirectly using information from the test set during development.
You should not repeatedly study the exact final exam questions and then claim the exam measures completely unseen performance.
A House Price Example
Suppose we have 10,000 historical house records.
We could create a simple example split:
The model learns from the training data.
We use the validation data while developing and choosing the model.
Finally, we evaluate the selected model on the test data.
The Test Data Should Be Unseen
When we say the test data is unseen, we mean the model should not have used those examples as training examples.
More importantly, we should avoid repeatedly making model-development decisions based on the test results.
Model learns from examples.
Development decisions are made.
Final unseen evaluation.
What Does the Test Result Tell Us?
Suppose the final model achieves an accuracy of 92% on the test dataset.
That gives us evidence about how the model performs on data that was kept separate from the training and development process.
The final model correctly classified 92% of the test examples.
The exact metric depends on the Machine Learning problem. Accuracy is only one possible metric.
Test Performance Can Reveal Generalization
One of the important things we care about is whether the model learned useful patterns rather than simply fitting the training examples.
Very strong performance on examples the model learned from.
A large difference may indicate that the model does not generalize well.
The difference between training and unseen-data performance can provide useful information about the model.
Don't Keep Rechecking the Test Set
Consider this workflow:
Train model → test → change model → test again → change model → test again.
This turns the test set into another development dataset.
A better workflow is:
Train → validate → improve and choose → freeze the final approach → test once for final evaluation.
Test Data Is Not Always a Perfect Representation
A test score is useful, but it does not magically guarantee real-world performance.
If the test data is poorly collected or does not represent the environment where the model will be used, a strong test score can still be misleading.
A model designed for worldwide customers should not be evaluated only on customers from one small region and then assumed to work equally well everywhere.
Test Data in Python
In a typical workflow, the test set is separated before training and development decisions are made.
from sklearn.model_selection import train_test_split
X_train, X_test, y_train, y_test = train_test_split(
X,
y,
test_size=0.2,
random_state=42
)
model.fit(X_train, y_train)
predictions = model.predict(X_test)
Here, the model is fitted using the training data and predictions are then generated for the separate test data.
In a full training/validation/test workflow, the exact splitting strategy can be more involved.
The Final Exam Analogy
Learn the concepts and practice examples.
Identify weaknesses and improve your approach.
Measure performance on questions kept aside for the final evaluation.
The Three Roles
Used to fit the model.
Used during development.
Used for the final evaluation.
Protect the Test Set
The test dataset should remain separate from model training and repeated development decisions. Its job is to provide a final estimate of performance on unseen data.
Which Dataset Should Be Used for the Final Evaluation?
You have trained several models and used validation data to select the final approach.
Which dataset should you use now to estimate the final performance?
The test dataset.
It should be kept separate so that the final evaluation is based on examples that were not used during model development.