Train-Test Split Concepts
Why split data?
Section titled “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
Section titled “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
Section titled “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
Section titled “Stratification”If your target classes are imbalanced, use stratify=y so train/test have similar class distribution.
Time-series splits
Section titled “Time-series splits”For time series, you often do not shuffle. You train on past and test on future.
Good practice
Section titled “Good practice”- Keep a final test set untouched.
- Use cross-validation on training data for tuning.
- Put preprocessing inside a pipeline.
Visualize it
Section titled “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
Section titled “🧪 Try It Yourself”Exercise 1 – Basic train/test split
Section titled “Exercise 1 – Basic train/test split”Exercise 2 – Keep class balance with stratify
Section titled “Exercise 2 – Keep class balance with stratify”Exercise 3 – Reproducible splits with random_state
Section titled “Exercise 3 – Reproducible splits with random_state”Once your split strategy is settled, wire scaling, encoding, and the model together with a Preprocessing Pipeline (scikit-learn).
pch.coffeeTagline
pch.coffeeCtapch.feedbackHeading
pch.feedbackSubheading