beginnermachine-learning

Train-Test Split

Never test your model on the data it trained on. That is like giving students the exam questions during lectures.

Key Points

What it is

Train-test split is the simple but essential technique of dividing your dataset into two parts. The training set (usually 70-80% of the data) is used to teach the model. The test set (the remaining 20-30%) is kept completely hidden during training. After the model is trained, you evaluate it on the test set to see how well it performs on new, unseen data. This tells you if your model actually generalizes or just memorized the training data.

Why it exists

If you evaluate a model on the same data it learned from, you have no idea if it actually learned the general pattern or just memorized the answers. A decision tree could grow a leaf for every single training example and get 100% accuracy on training, but be useless on new data. The test set simulates the real world where you ask the model to predict something it has never seen. Without a test set, you are fooling yourself.

What it measures

The train-test split measures generalization error. The difference between training accuracy and test accuracy tells you how much overfitting is happening. A small gap (e.g., 95% train, 93% test) means good generalization. A large gap (99% train, 70% test) means severe overfitting. If both are low (60% train, 58% test), the model is underfitting.

Intuition

Think of studying for a math test. Your teacher gives you a practice problem set (training data). You learn from those problems. The real test (test data) has new problems you have never seen. If you just memorized the practice problems but dont understand the concepts, you will fail the real test. The train-test split is like your teacher making sure the test is different from the practice set. That way they know if you actually learned math or just memorized answers.

When to use

When not to use

Common mistakes

In practice

Train-test split is the first step in any supervised learning pipeline. In Python with scikit-learn: `from sklearn.model_selection import train_test_split`. You call `X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)`. Then you train on `X_train, y_train`, and evaluate on `X_test, y_test`. Never touch the test set until the very end when you compute final metrics.

Was this concept helpful?

Spot an error or gap? Suggest an edit →

Related concepts

cross-validationoverfitting-underfittingvalidation-set
Chirp at me!