A tree's depth is how many questions it may ask in a row. DecisionTreeClassifier(max_depth=3) stops after 3. max_depth=None means no limit: the tree keeps asking until every group it ends with is all 1s or all 0s. A deeper tree carves the customers into smaller and smaller groups.
Does that help? Score the tree twice to find out. model.score(X, y) predicts every row of X and gives back the share it got right, a number from 0 to 1. On the training rows, the ones the tree learned from, a deeper tree always looks better. The test rows are customers it has never seen, so they tell you how it will do on new customers.
tree = DecisionTreeClassifier(max_depth=2, random_state=0)
tree.fit(X_train, y_train)
print(tree.score(X_train, y_train), tree.score(X_test, y_test))
When the train score keeps climbing but the test score doesn't follow, the tree is overfitting: it learns the training customers by heart, noise included, instead of a rule that carries over to new ones. In this lesson you try four depths in one for loop and print both scores for each:
Depth 1 train: 0.76 test: 0.653 Depth 3 train: 0.813 test: 0.627 Depth 10 train: 0.987 test: 0.68 Depth None train: 1.0 test: 0.667 The last tree grew 14 questions deep
The last line comes from the lesson's check: after the loop, model holds the last tree, the one with no limit.
Your task: the customers are loaded and split for you, with five features. Loop over the depths [1, 3, 10, None]. Inside the loop, make a tree with max_depth=depth and random_state=0 in model, fit it on the training rows, keep its train and test scores rounded to 3 digits in train_score and test_score, and print one line per depth.
Assignment
Follow the Coding Tutorial to grow four trees of different depths in one loop and compare their train and test scores.
Hint
Indent every line under the for line by 4 spaces so it runs once per depth. The train scores rise to 1.0; the test scores stay between 0.627 and 0.68.
Free plan: 30 tutor messages. PRO is unlimited.
