Why split data?
When you evaluate a model, you want to test on data it hasnโt seen.
- Train set: used to learn patterns
- Test set: used only for final evaluation
This simulates real-world performance.
The biggest danger: data leakage
Leakage happens when information from the test set influences training.
Examples:
- Scaling using mean/std computed on the full dataset
- Filling missing values using overall mean (including test)
- Feature engineering that uses future information
Basic split with scikit-learn
import pandas as pd
from sklearn.model_selection import train_test_split
X = pd.DataFrame({"age": [20, 21, 22, 23, 24], "score": [80, 85, 78, 90, 88]})
y = pd.Series([0, 0, 0, 1, 1])
X_train, X_test, y_train, y_test = train_test_split(
X, y,
test_size=0.2,
random_state=42,
stratify=y,
)
print(X_train)
print(X_test)Stratification
If your target classes are imbalanced, use stratify=y so train/test have similar class distribution.
Time-series splits
For time series, you often do not shuffle. You train on past and test on future.
Good practice
- Keep a final test set untouched.
- Use cross-validation on training data for tuning.
- Put preprocessing inside a pipeline.
Visualize it
flowchart LR A["Full dataset"] --> B["train_test_split(test_size=0.2)"] B --> C["Train set (80%)"] B --> D["Test set (20%, untouched)"] C --> E["Fit model"] E --> F["Evaluate on D"]
๐งช Try It Yourself
Exercise 1 โ Basic train/test split
Tab indents. Press Escape, then Tab, to leave the editor. Control plus Enter runs the code; Control plus Shift plus Enter submits it.
Press Run to see what your code prints.Exercise 2 โ Keep class balance with stratify
Tab indents. Press Escape, then Tab, to leave the editor. Control plus Enter runs the code; Control plus Shift plus Enter submits it.
Press Run to see what your code prints.Exercise 3 โ Reproducible splits with random_state
Tab indents. Press Escape, then Tab, to leave the editor. Control plus Enter runs the code; Control plus Shift plus Enter submits it.
Press Run to see what your code prints.Check your understanding
Which of these is an example of data leakage?
Computing statistics on the full dataset lets information from the test set influence training.
Show answer
A โ Scaling with mean and std computed on the full dataset โ Computing statistics on the full dataset lets information from the test set influence training.
What does test_size=0.2 mean for 5 rows?
test_size is the fraction held out for testing. random_state=42 makes the split reproducible.
Show answer
A โ About 20 percent of the rows go to the test set โ test_size is the fraction held out for testing. random_state=42 makes the split reproducible.
When should you use stratify=y?
stratify keeps the class proportions similar in both parts of the split.
Show answer
A โ When target classes are imbalanced and train and test should have similar class distribution โ stratify keeps the class proportions similar in both parts of the split.
How does the lesson say to split time series data?
Shuffling a time series would let the model peek at the future.
Show answer
A โ Do not shuffle, train on the past and test on the future โ Shuffling a time series would let the model peek at the future.
Next
Once your split strategy is settled, wire scaling, encoding, and the model together with a Preprocessing Pipeline (scikit-learn).