Data Science · Chapter 25 of 43

Train / Test Split

SPLIT your data into TRAIN (fit the model) and TEST (evaluate honestly). Typical ratios: 80/20 or 70/30.

Never peek at the test set while iterating — that inflates your reported accuracy.

Example 1 (python)
from sklearn.model_selection import train_test_split
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)

Reproducible split.

Example 2 (python)
# For time series, split by TIME not randomly

Chronological split for time-ordered data.

Key points

  • Always split before training.
  • 80/20 or 70/30 is typical.
  • random_state for reproducibility.
  • For time series, split chronologically.
💡 Note: For CLASSIFICATION with imbalanced classes, use `stratify=y` to keep the class ratio in both splits.

📝 Quick Quiz

1. The test set is used to:

2. random_state helps:

3. For time series, split by: