Training and testing
- Machine Learning course
- Beginner
- Lesson 4/112
- 5 min
The model is trained on training data and its performance is measured on test data.
Why separate test data?
If you measure the model with the data you trained it on, you can't tell whether it has just memorised it. So the data is split in two: the model learns with the training set and its success on unseen examples is measured with the test set.
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)test_size=0.2 sets aside 20% of the data for testing; random_state makes the split the same on every run.
The four returned parts
train_test_split returns four arrays and the order matters: first the two X parts, then the two y parts.
model.fit(X_train, y_train) # training only
print(model.score(X_test, y_test)) # test onlyTest data must never be used in training; otherwise the measured success doesn't reflect reality. This is called data leakage.
Practice Split the data
Split the 20-example data with test_size=0.25 and random_state=0. Print the lengths of the training and test sets separated by a space.
Starter code
from sklearn.model_selection import train_test_split
X = [[i] for i in range(20)]
y = [i % 2 for i in range(20)]
# buraya yazSample solution
from sklearn.model_selection import train_test_split
X = [[i] for i in range(20)]
y = [i % 2 for i in range(20)]
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.25, random_state=0)
print(len(X_train), len(X_test))Expected output
15 5
Questions and answers
Review the lesson questions together with their answers. To test yourself, solve the lesson interactively.
1 With 100 examples and <code>test_size=0.25</code>, how many examples remain for training?
- 25
- 100
- 50
- 75 Correct answer
Explanation: 25% (25 examples) goes to the test set and the remaining 75 are for training.
2 Set aside 30% of the data for testing.
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=____, random_state=0)
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.3, random_state=0)
Explanation: test_size takes a ratio between 0 and 1 or an integer count; 0.3 means 30 percent.
3 Match the parts with their meanings.
| Term | Meaning |
|---|---|
X_train |
Training inputs |
y_train |
Training labels |
X_test |
Test inputs |
y_test |
Test labels |