bhylu.com

Advertisements

Train/Test

A train/test split separates learning data from a final evaluation sample. Keep the test set untouched while deciding features, preprocessing, and model settings.

Understand the idea

A random split assumes observations are exchangeable for the task. Time series or repeated people may need chronological or grouped splitting instead. This example prints disjoint row identifiers rather than a model score so the separation is visible. A fixed random_state makes the demonstration reproducible, not representative by itself.

Where to try it

Run locally with scikit-learn and NumPy installed in your virtual environment (py -m pip install scikit-learn numpy). These examples are executed during course review, but the online runner does not currently preload scikit-learn. The small data is for learning the API, not evidence of production model quality.

A worked example

Python

from sklearn.model_selection import train_test_split
train, test = train_test_split(list(range(12)), test_size=0.25, random_state=7)
print(len(train), len(test))
print(set(train).isdisjoint(test))

Read the result

There are 9 training rows and 3 test rows, and their identifier sets are disjoint.

Try one change

Describe a split for weekly demand where future weeks must never influence earlier predictions.

For package options, see the scikit-learn documentation.