EXERCISE
1Testing a model on the same data it learned from is like grading your own exam — you will always get 100%, but you have learned nothing.
Save
Imagine a student who memorises every answer in the textbook but cannot solve a single new problem. That student the textbook. In ML, a model that scores perfectly on but fails on new data has the exact same problem.
The solution: split your data.
import numpy
numpy.random.seed(2)
x = numpy.random.normal(3, 1, 100)
y = numpy.random.normal(150, 40, 100) / x
# 80% for training, 20% for testing
train_x = x[:80]
train_y = y[:80]
test_x = x[80:]
test_y = y[80:]
print(f"Training samples: {len(train_x)}")
# Output: Training samples: 80
print(f"Testing samples: {len(test_x)}")
# Output: Testing samples: 20
Why 80/20?
| Split | Training | Testing | Use Case |
|---|---|---|---|
| 80/20 | 80% | 20% | Most common, good default |
| 70/30 | 70% | 30% | Small datasets (more test = safer) |
| 90/10 | 90% | 10% | Very large datasets (millions of rows) |
> 💡 Key Insight: The test set must be data the model has NEVER seen during training. If even one test point leaks into training, your accuracy score is inflated and meaningless. This concept — keeping train and test strictly separate — is the foundation of honest ML evaluation.