Start learning free

LESSON 32 OF 165 · MACHINE LEARNING: YOUR FIRST MODEL

Hold Some Rows Back

Practice

A model is only worth something if it does well on new customers, people it has never seen. If you score it on the rows it learned from, it can partly remember their answers, and the score looks better than the truth. So before it learns anything, you hold some rows back: the test rows. The model learns from the rest, the training rows, and later the test rows play the new customers.

train_test_split(X, y, test_size=…, random_state=…), from sklearn.model_selection, shuffles the rows and cuts them in two. It gives back four parts, in this order: the training rows of X, the test rows of X, the training answers of y, and the test answers of y. test_size is the share to hold back. random_state fixes the shuffle, so every run holds back the same rows. Holding back 20% of the customers looks like this:

X_a, X_b, y_a, y_b = train_test_split(X, y, test_size=0.2, random_state=1)
print(X_a.shape)   # (240, 5): 240 rows to learn from
print(X_b.shape)   # (60, 5): 60 rows held back

.shape gives back a pair: (rows, columns). .head() gives back the first 5 rows, and .to_string() turns them into text you can print. On a column of 0s and 1s, .mean() gives back the share of 1s: pd.Series([1, 0, 1, 1]).mean() is 0.75. Your script must print this. The last two lines come from the lesson's check: no test row is also a training row, and the two parts hold all 300.

All rows: (300, 5)
Train: (225, 5)
Test: (75, 5)
     visits  orders  spent  days_since_last  returned_item
208      18       6    357                8              0
188       5       2    310               20              0
12        8       1     22              140              1
221       2       3    104              140              0
239       9       1      0               87              0
Bought again in train: 0.578
Bought again in test: 0.653
Test rows that are also training rows: 0
Training rows + test rows: 300

Your task: the starting code loads customers.csv, picks the features X and the label y, and prints their shape. Split them with train_test_split into X_train, X_test, y_train, y_test, holding back 25% with random_state=0. Then print the shapes of X_train and X_test, the first 5 test rows, and the share of buyers in y_train and in y_test, rounded to 3 digits.


Assignment
Follow the Coding Tutorial to split the customers into training and test rows, then print what each part looks like.


Hint
The four parts always come back in the same order: X_train, X_test, y_train, y_test. Both parts of X first, then both parts of y. The test rows are picked at random, so the labels on the left are not the last 75.


STEP 1 OF 6
    Ctrl + Enter to check