CodeBlanks Logo Icon CodeBlanks

Train-test split

Data is split into train and test sets with train_test_split.

Why a separate test?

A model's real value is what it does on data it has never seen. train_test_split shuffles the data randomly and splits it; shuffle=True is the default.

from sklearn.model_selection import train_test_split
X_tr, X_te, y_tr, y_te = train_test_split(X, y, test_size=0.2,
                                          random_state=42, stratify=y)

With time series shuffling is wrong: it means learning from the future to predict the past. There the first part goes to training and the last part to testing with shuffle=False.

random_state and reproducibility

Different splits give different scores; with small datasets the difference can be large. Fixing random_state makes the result reproducible:

for rs in [0, 1, 2]:
    X_tr, X_te, y_tr, y_te = train_test_split(X, y, test_size=0.3, random_state=rs)
    print(rs, model.fit(X_tr, y_tr).score(X_te, y_te))

Seeing how much the score moves with the split teaches you not to over-trust a single number; cross-validation solves this problem.

Practice Ordered split

Split the ordered 10-element data with test_size=0.3, shuffle=False and print the test inputs with X_test.ravel().

Starter code

model.py
import numpy as np
from sklearn.model_selection import train_test_split

X = np.arange(10).reshape(-1, 1)
y = np.arange(10) * 2

# buraya yaz

Sample solution

model.py
import numpy as np
from sklearn.model_selection import train_test_split

X = np.arange(10).reshape(-1, 1)
y = np.arange(10) * 2

X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.3, shuffle=False)
print(X_test.ravel())

Expected output

[7 8 9]

Questions and answers

Review the lesson questions together with their answers. To test yourself, solve the lesson interactively.

1 If you'll predict next month from daily sales data, how do you split?

  • Without a test set
  • Last days training, old days testing
  • Without shuffling: old days for training, last days for testing Correct answer
  • With random shuffling

Explanation: Time order must be kept; the model may learn only from the past.

2 Split the time series without shuffling.

Question
train_test_split(X, y, test_size=0.2, ____=False)
Answer
train_test_split(X, y, test_size=0.2, shuffle=False)

Explanation: shuffle=False keeps the order; the last 20% becomes the test.

3 Match the parameters.

Term Meaning
test_size Test share
random_state Reproducible split
stratify Keeps class proportion
shuffle=False Keeps the order

All lessons in Beginner

13 Project: Classification

  1. Preparing data
  2. Training model
  3. Prediction
  4. Evaluation