Start learning free

LESSON 33 OF 165 · MACHINE LEARNING: YOUR FIRST MODEL

Fit, Then Predict in Python

Practice

A decision tree guesses an answer by asking yes/no questions about a row, one after another: fewer than 2 orders? More than 100 days since the last visit? Each answer leads to the next question, until the tree reaches a guess. You don't write those questions. The tree finds them itself, in rows whose answers you know.

DecisionTreeClassifier(max_depth=…, random_state=…), from sklearn.tree, gives back a new, empty model: your settings, no questions yet. max_depth is how many questions it may ask in a row, and random_state makes it build the same tree on every run. model.fit(X, y) is the learning: it finds the questions that best link the rows in X to their answers in y. model.predict(rows) gives back an array of guesses, one 0 or 1 for each row you pass it. For example, a tree that learns from 4 students how many hours of study it takes to pass:

hours = pd.DataFrame({"hours": [1, 2, 8, 9]})
passed = pd.Series([0, 0, 1, 1])
tree = DecisionTreeClassifier(max_depth=1, random_state=0)
tree.fit(hours, passed)
print(tree.predict(pd.DataFrame({"hours": [3, 7]})))   # [0 1]

From the 4 students the tree learned one question, "5 hours or less?", so it guesses 0 (fails) for 3 hours and 1 (passes) for 7. X_test[:5] gives back the first 5 test rows, and rows["predicted"] = … adds a column to a table, like a new key in a dictionary. Your script must print the 5 test rows with the tree's guess and the real answer side by side. The last two lines come from the lesson's check:

     visits  orders  spent  days_since_last  returned_item  predicted  real
208      18       6    357                8              0          1     1
188       5       2    310               20              0          1     1
12        8       1     22              140              1          1     0
221       2       3    104              140              0          0     1
239       9       1      0               87              0          1     0
Rows the tree learned from: 225
Tree depth: 3

Your task: the starting code loads the customers, splits them, and prints the first 5 test rows. Make a DecisionTreeClassifier with max_depth=3 and random_state=0 in model, fit it on X_train and y_train, keep its guesses for X_test[:5] in predictions, and add the columns "predicted" and "real" to rows. Do not print the last two lines yourself. The check prints them after your script ends, using your fitted model. Your script prints only the table, once, after both new columns are added.


Assignment
Follow the Coding Tutorial to fit a decision tree on the training rows and set its guesses for 5 test rows next to the real answers.


Hint
fit gets rows and their answers; predict gets rows only and gives back guesses. Fit first: an empty tree has no questions to guess with.


STEP 1 OF 5
    Ctrl + Enter to check

    Ml Fit Predict in Python

    Keep going with Python1,600+ step-by-step lessons, from your first line of code to coding interviews. Free to start.
    Start learning free