Last updated

Train / Test Split

Definition: To check if a model really learned (rather than just memorised), you split your data: train on most of it, then test on the part the model has never seen.

Why split the data?

If you test a model on the same data it learned from, of course it does well — that is like giving a student the exam answers in advance. Testing on unseen data shows how it performs in the real world.

Example — scikit-learn makes it one line

This loads scikit-learn, the main ML library, on the first Run:

from sklearn.model_selection import train_test_split
X = list(range(1, 11))      # 1..10
y = [v * 2 for v in X]      # the answer is double

X_train, X_test, y_train, y_test = train_test_split(
    X, y, test_size=0.2, random_state=1)

print("Train on:", X_train)
print("Test on: ", X_test)

test_size=0.2 keeps 20% for testing. random_state makes the split repeatable.

💡 Rule of thumb: a common split is 80% training and 20% testing.

Try it Yourself
Output

          
Ad · responsive