Codiqo

Understanding AI · lesson 22 of 60 · about 5 minutes

Training Data vs Test Data

Hold some examples back so you can test fairly. Learn how data is split and why.

What you'll learn

  • Split data into training and test sets
  • Explain why test data must stay unseen
  • Spot when test data has leaked into training

Keep some back

Before training, set aside some examples, often about one in five, as the test set. Train only on the rest. At the end, the test set shows how the model copes with examples it has never seen.

If any test examples sneak into training, the test is spoiled, just like seeing the exam before you sit it.

Splitting 10 examples: the first 8 for training, the last 2 for testing. `[:8]` takes items up to position 8.

examples = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
training = examples[:8]
test = examples[8:]
print(training)
print(test)

Output

[1, 2, 3, 4, 5, 6, 7, 8]
[9, 10]

Tip

In real projects the examples are shuffled before splitting, so the test set is a fair mix.

Now try it yourself

This lesson is free in Codiqo, with 6 questions on what you just read. You can only learn to code by writing code, so it ends with a short program you write yourself, which we run and check straight away.