Python Central HubSign in

Data Analytics with Python / Phase 4: Data Preprocessing and Cleaning

Train-Test Split Concepts

Understand why we split data, what leakage is, and how to split properly for ML workflows.

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

Python
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

diagram Train/test split mermaid
The full dataset is split once; the model learns only from train and is evaluated only on the untouched test set.
sketch Splitting one dataset into train and test p5.js
The same rows, rearranged into an 80/20 split โ€” train teaches the model, test grades it.

๐Ÿงช Try It Yourself

Exercise 1 โ€“ Basic train/test split

exerciseYour turn

Tab indents. Press Escape, then Tab, to leave the editor. Control plus Enter runs the code; Control plus Shift plus Enter submits it.

Output
Press Run to see what your code prints.

Exercise 2 โ€“ Keep class balance with stratify

exerciseYour turn

Tab indents. Press Escape, then Tab, to leave the editor. Control plus Enter runs the code; Control plus Shift plus Enter submits it.

Output
Press Run to see what your code prints.

Exercise 3 โ€“ Reproducible splits with random_state

exerciseYour turn

Tab indents. Press Escape, then Tab, to leave the editor. Control plus Enter runs the code; Control plus Shift plus Enter submits it.

Output
Press Run to see what your code prints.

Check your understanding

quizCheck yourself
  1. Which of these is an example of data leakage?

    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.

  2. What does test_size=0.2 mean for 5 rows?

    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.

  3. When should you use stratify=y?

    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.

  4. How does the lesson say to split time series data?

    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).

Was this page helpful?

Let us know how we did