MACHINE LEARNING • LESSON 4

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.

THE CORE IDEA

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.

01

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.

02

Training, Validation, and Test

TRAINING Learn

The model learns patterns from these examples.

VALIDATION Choose

Used during development to compare choices.

TEST Evaluate

Used for the final evaluation.

03

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.

The test set should be treated like a final exam.

You should not repeatedly study the exact final exam questions and then claim the exam measures completely unseen performance.

04

A House Price Example

Suppose we have 10,000 historical house records.

We could create a simple example split:

TRAINING 8,000
VALIDATION 1,000
TEST 1,000

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.

05

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.

Training Data

Model learns from examples.

Validation Data

Development decisions are made.

Test Data

Final unseen evaluation.

06

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.

FINAL TEST RESULT 92% Accuracy

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.

07

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.

EXAMPLE A Training: 99%

Very strong performance on examples the model learned from.

TEST Test: 72%

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.

08

Don't Keep Rechecking the Test Set

Consider this workflow:

Bad 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:

Better workflow

Train → validate → improve and choose → freeze the final approach → test once for final evaluation.

09

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.

Example

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.

10

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.

11

The Final Exam Analogy

TRAINING Study

Learn the concepts and practice examples.

VALIDATION Mock Exam

Identify weaknesses and improve your approach.

TEST Final Exam

Measure performance on questions kept aside for the final evaluation.

12

The Three Roles

TRAINING Learn

Used to fit the model.

VALIDATION Choose

Used during development.

TEST Evaluate

Used for the final evaluation.

KEY IDEA

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.

QUICK CHECK

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?

Answer

The test dataset.

It should be kept separate so that the final evaluation is based on examples that were not used during model development.

NEXT TOPIC

Training vs Validation vs Test

We have now examined each dataset separately. Next, we will put all three together and compare their roles directly.