You are viewing a public preview
Sign in to save your progress, do assignments, join discussions and get feedback.
After this unit you will: be able to read the train_test_split cell and know what test_size decides.
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=1009
)
x and y; the output is four objects in the order x_train, x_test, y_train, y_test.test_size=0.2: 20% of the rows go to the test set, and the remaining 80% go to the training set.By the end of this lesson you can:
train_test_split to divide x and y into training and test setstest_size=0.2 does and estimate the size of each setrandom_state provides itKey idea:
train_test_splitrandomly holds back part of the data as a test set; fixingrandom_statekeeps that test set identical across runs, so scores from different trainings can be compared fairly.
The model must be judged on data it has never seen. So before preprocessing or training, we split the rows into a training set (to learn from) and a test set (to evaluate on). You could do this by hand with pandas, but scikit-learn's train_test_split makes it much easier.
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=1009
)
x and y.test_size=0.2: 20% of the rows go to the test set; the other 80% form the training set.x_train, x_test, y_train, y_test.For diabetes.csv with 768 rows: 768 x 0.2 = 153.6, which scikit-learn rounds up to 154 test rows, leaving 614 training rows.
The split keeps features and labels aligned: row 498 of x goes to the same set as row 498 of y, so every patient's measurements stay matched with their own Outcome. The rows are also shuffled before splitting by default, which matters when a file is sorted (for example, all patients with diabetes at the end).
The labels used to train the model are in y_train; y_test is kept aside to check predictions later.
Each call to train_test_split splits the data randomly. The function guarantees the proportions (20% / 80%), but not which rows end up where. Call it 10 times and you get 10 different test sets.
That is a real problem for comparison. Suppose the first training run scores 80% accuracy and the second 81%. If the two runs were evaluated on different test sets, you cannot say the second model is better: 81% on one test set is no guarantee of improvement over 80% on another.
To benchmark models consistently, we need a fixed test set. Setting random_state to a number does that: as long as you use the same number (here 1009), every run produces the same split. The split is still random, but the randomness is fixed. Because the test set is fixed, the training set is fixed too.
Split the diabetes data and check the sizes.
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=1009)
print(x_train.shape, x_test.shape)
print(y_train.shape, y_test.shape)
print(len(x_train) + len(x_test))
Expected output:
(614, 8) (154, 8)
(614,) (154,)
768
Step by step:
x_train and y_train have the same number of rows, and so do x_test and y_test.A quick self-check: run the cell twice and print x_test.index[:5]; with random_state=1009 the indices are identical each time.
x_train, y_train, x_test, y_test = ... silently puts the wrong objects in the wrong variables.test_size as the training share. test_size=0.2 means 20% for testing, not 20% for training.random_state, a better score may just be an easier test set.train_test_split(x, y, test_size=0.2, random_state=1009) splits rows into training and test sets.x_train, x_test, y_train, y_test.test_size=0.2: 154 test rows and 614 training rows.random_state fixes it so every run uses the same test set.Flashcards · 3.P2 Splitting Train/Test with train_test_split
8 wordsscikit-learn function that randomly splits features and labels into training and test sets.
The proportion (or number) of rows reserved for the test set.
train_test_split returns x_train, x_test, y_train, y_test, always in this order.
The rows the model learns from.
Rows held back to evaluate the model on data it has never seen.
The labels used together with x_train to train the model.
A seed that fixes the random split, so every run produces the same training and test sets.
Comparing models on exactly the same test set so score differences reflect the models.