Train-Test Split
Train-Test Split
Dividing your dataset into training and testing sets — the foundation of evaluating ML models.
What is Train-Test Split?
Train-Test Split is a Machine Learning technique used to divide the dataset into two parts: one for training the model and the other for testing its performance.
Why is Train-Test Split Needed?
If we train and test our model on the same data, the model will memorize the data instead of learning from it. This leads to:
Without Splitting
- Overfitting (model memorizes data)
- False high accuracy
- No way to test real performance
- Model fails on new data
With Train-Test Split
- Honest evaluation
- Detects overfitting
- Measures generalization
- Reliable model performance
Concept of Train-Test Split
The full dataset is split into two parts:
Training Set
Used to train the model. The model learns patterns from this data.
Usually: 70%–80% of the dataset.
Testing Set
Used to evaluate the model after training. The model has never seen this data.
Usually: 20%–30% of the dataset.
Common Train-Test Ratios
| Ratio | Training | Testing | Best Use Case |
|---|---|---|---|
| 80 / 20 | 80% | 20% | Most common — standard |
| 70 / 30 | 70% | 30% | Smaller datasets |
| 90 / 10 | 90% | 10% | Very large datasets |
| 60 / 20 / 20 | 60% | 20% Validation + 20% Test | When validation set is also needed |
Steps in Train-Test Split
Process Flow
- Step 1: Load and clean the dataset.
- Step 2: Separate features (X) and target (y).
- Step 3: Decide split ratio (e.g., 80/20).
- Step 4: Use
train_test_split()to divide data. - Step 5: Train the model on training data.
- Step 6: Evaluate model on testing data.
Python Example
pip install pandas scikit-learn
import pandas as pd
from sklearn.model_selection import train_test_split
# Sample dataset
data = {
"Age": [25, 30, 35, 40, 45, 50, 55, 60],
"Salary": [25000, 30000, 40000, 50000, 60000, 70000, 80000, 90000],
"Buy": [0, 0, 0, 1, 1, 1, 1, 1]
}
df = pd.DataFrame(data)
# Separate features and target
X = df[["Age", "Salary"]]
y = df["Buy"]
# Split data into train and test
X_train, X_test, y_train, y_test = train_test_split(
X, y,
test_size=0.2,
random_state=42
)
print("Training Features:\n", X_train)
print("Testing Features:\n", X_test)
Important Parameters of train_test_split()
| Parameter | Description |
|---|---|
test_size | Proportion of dataset for testing (e.g., 0.2 = 20%). |
train_size | Proportion of dataset for training (optional). |
random_state | Ensures reproducibility — same split every run. |
shuffle | Whether to shuffle data before splitting (default = True). |
stratify | Maintains class distribution (used in classification). |
Stratified Train-Test Split
When the dataset is imbalanced (e.g., 90% Class A, 10% Class B), a normal split may put all Class B examples in one set. To prevent this, we use stratification to keep the same class ratio in both sets.
X_train, X_test, y_train, y_test = train_test_split(
X, y,
test_size=0.2,
stratify=y,
random_state=42
)
What is random_state?
random_state is a seed value that ensures the same split happens every time you run the code. This is useful for:
- Reproducible experiments.
- Debugging models.
- Comparing different ML algorithms fairly.
Train-Validation-Test Split (3-Way Split)
Sometimes, we split data into three parts instead of two — adding a Validation Set to fine-tune the model.
| Set | Purpose | Usual % |
|---|---|---|
| Training Set | Train the model | 60% |
| Validation Set | Tune hyperparameters | 20% |
| Testing Set | Final evaluation | 20% |
from sklearn.model_selection import train_test_split
# Step 1: Split into train+val and test
X_temp, X_test, y_temp, y_test = train_test_split(X, y, test_size=0.2, random_state=42)
# Step 2: Split train+val into train and validation
X_train, X_val, y_train, y_val = train_test_split(X_temp, y_temp, test_size=0.25, random_state=42)
# 0.25 of 80% = 20% of original data → Validation
Real-Life Analogy
Train-Test Split = Studying vs Exam
Think of the training set as the chapters you study, and the test set as the exam questions. If you only practice the same questions repeatedly (no separate test), you can't tell whether you truly learned the topic or just memorized answers.
Visual Representation
| Training (80%) | Testing (20%) |
|---|---|
| Model learns patterns here | Model is evaluated here |
How Train-Test Split Helps Detect Overfitting
Overfitting
- High training accuracy
- Low testing accuracy
- Model memorizes data
Good Fit
- High training accuracy
- High testing accuracy
- Model generalizes well
Underfitting
- Low training accuracy
- Low testing accuracy
- Model is too simple
Common Mistakes to Avoid
random_state → splits change every run, making results inconsistent.
stratify on imbalanced classification problems.
Best Practices
Quick Tips
- Always set
random_statefor reproducibility. - Use
stratify=yfor classification problems. - Split before scaling or encoding.
- Use a separate validation set when tuning hyperparameters.
- For small datasets, use k-fold cross-validation instead.
- Keep the test set untouched until the final evaluation.
Importance of Train-Test Split
Honest Evaluation
- Measures real-world performance
- Detects overfitting
Prevents Bias
- Test data is unseen
- Fair model assessment
Improves Reliability
- Helps fine-tune models
- Ensures stable accuracy
Reproducible Results
- Consistent experiments
- Allows fair comparison
Mathematical View
If a dataset has N samples, then:
Where t = test_size (e.g., 0.2 means 20%).
Key Takeaway
Train-Test Split is the foundation of Machine Learning evaluation. By dividing your dataset into training and testing parts, you ensure that your model not only learns well but also performs reliably on new, unseen data.