Python ML Decision Trees
A decision tree is a model that sorts rows with yes-or-no questions. Each question splits the rows, and each leaf gives one answer.
Fit a small tree
max_depth=3 keeps the tree short. The tree fits the training rows and then scores the test rows.
from sklearn.datasets import load_breast_cancer
from sklearn.model_selection import train_test_split
data = load_breast_cancer()
X_train, X_test, y_train, y_test = train_test_split(data.data, data.target, test_size=0.2, random_state=42)
from sklearn.tree import DecisionTreeClassifier
tree = DecisionTreeClassifier(max_depth=3, random_state=42)
tree.fit(X_train, y_train)
print(round(tree.score(X_test, y_test), 3))Read the depth and the leaves
get_depth() gives the longest chain of questions. get_n_leaves() counts the final boxes.
from sklearn.datasets import load_breast_cancer
from sklearn.model_selection import train_test_split
data = load_breast_cancer()
X_train, X_test, y_train, y_test = train_test_split(data.data, data.target, test_size=0.2, random_state=42)
from sklearn.tree import DecisionTreeClassifier
tree = DecisionTreeClassifier(max_depth=3, random_state=42)
tree.fit(X_train, y_train)
print(tree.get_depth())
print(tree.get_n_leaves())A deeper tree fits more
max_depth=8 lets the tree ask more questions. The training score rises, and the test score shows the cost.
from sklearn.datasets import load_breast_cancer
from sklearn.model_selection import train_test_split
data = load_breast_cancer()
X_train, X_test, y_train, y_test = train_test_split(data.data, data.target, test_size=0.2, random_state=42)
from sklearn.tree import DecisionTreeClassifier
tree = DecisionTreeClassifier(max_depth=8, random_state=42)
tree.fit(X_train, y_train)
print(round(tree.score(X_train, y_train), 3))
print(round(tree.score(X_test, y_test), 3))Example
from sklearn.datasets import load_breast_cancer
from sklearn.model_selection import train_test_split
from sklearn.tree import DecisionTreeClassifier
data = load_breast_cancer()
X_train, X_test, y_train, y_test = train_test_split(data.data, data.target, test_size=0.2, random_state=42)
tree = DecisionTreeClassifier(max_depth=3, random_state=42)
tree.fit(X_train, y_train)
print("Train accuracy:", round(tree.score(X_train, y_train), 3))
print("Test accuracy:", round(tree.score(X_test, y_test), 3))
print("Tree depth:", tree.get_depth())
print("Leaves:", tree.get_n_leaves())
print("First prediction:", tree.predict(X_test[:1])[0])
print("First true label:", y_test[0])Key points
- Decision tree: a model that splits data with yes-or-no questions
- Use DecisionTreeClassifier to build a tree for classification
- Depth: how many questions the tree asks in a row
- Leaf: a final box that gives the answer for the rows that reach it
- Use max_depth to limit the depth and stop the tree from memorising
- Use tree.get_depth() to read the depth of a fitted tree
- Use tree.get_n_leaves() to count the leaves of a fitted tree
In this example, the data holds breast cancer measurements with a benign or malignant label. train_test_split() holds back 20 percent of the rows for testing. DecisionTreeClassifier with max_depth=3 builds a short tree. fit() trains it on the training rows. score() then measures it on both sets. The tree depth is 3, it has 8 leaves, and the first test row is predicted correctly.
Note
A deep tree fits the training rows well and often fails on new rows.
Try it Yourself
Edit the code, then press Run.
Running is simulated · Python Playground
Exercise
What does max_depth limit in a decision tree?