A decision tree decides the way you might at the shop counter. It asks yes-or-no questions about a customer's numbers, one after another, like did they spend at most 150? Each answer leads to the next question or to a final answer, here 1 (comes back) or 0 (doesn't).
You don't write the questions yourself. DecisionTreeClassifier(max_depth=2, random_state=0) makes an empty tree that may ask at most 2 questions in a row. .fit(X_train, y_train) picks the questions that best split the customers who came back from those who didn't. export_text(model, feature_names=...) gives back those questions as text you can print, and model.predict(rows) gives back one answer per row.
small = DecisionTreeClassifier(max_depth=1, random_state=0)
small.fit(df[["visits"]], df["bought_again"])
print(export_text(small, feature_names=["visits"]))
That small tree asks just one question, about visits. In the printed text, each |--- line is one answer to a question, each extra | in front means one question deeper, and a class: line is a final answer. To predict by hand, start at the top and keep following the lines that are true for your customer.
This lesson's code prints the tree, the first test customer and what the tree says for them. The lesson's check adds the last three lines:
|--- spent <= 153.50
| |--- days_since_last <= 58.00
| | |--- class: 1
| |--- days_since_last > 58.00
| | |--- class: 0
|--- spent > 153.50
| |--- days_since_last <= 100.50
| | |--- class: 1
| |--- days_since_last > 100.50
| | |--- class: 0
spent days_since_last
208 357 8
The tree says: 1
Questions in a row: 2
Final answers (leaves): 4
Test accuracy: 0.68
Your task: the customers are loaded and split for you, with two features, spent and days_since_last, and the first test customer is in customer. Make a tree with max_depth=2 and random_state=0 in model, fit it, print its questions with export_text, then follow customer 208 down the tree yourself before you print what the model says.
Assignment
Follow the Coding Tutorial to grow a tree that asks two questions in a row, print its questions, and check one customer's answer against it.
Hint
Customer 208 spent 357, more than 153.50, so follow the lower half of the tree; there, 8 days is at most 100.50. model.predict gives back an array of answers, so [0] takes the first one.
Free plan: 30 tutor messages. PRO is unlimited.
