A decision rule is a human-readable if-then condition describing one path from a decision tree’s root to a leaf. Each rule sends examples into one subpopulation, making the tree’s classification easier to inspect. section 9.1.2
Read a decision tree as a complete set of rules:
- Follow each split from the root until reaching a leaf. section 9.1.2
- Combine the conditions encountered along that path with and. section 9.1.2
- Treat the leaf’s class as the rule’s result; the complete rule set covers every example without overlap. section 9.1.2
Iris classification
A compact tree can become three Python-style rules: petal width at most 0.8 predicts Iris-Setosa; otherwise, petal length at most 4.85 predicts Iris-Versicolour; larger values predict Iris-Virginica. section 9.1.2
Listing 9.1 Training and interpreting decision trees
from sklearn.datasets import load_iris
from sklearn.model_selection import train_test_split
iris = load_iris() #1
Xtrn, Xtst, ytrn, ytst = train_test_split(iris.data, iris.target,
test_size=0.15)
from sklearn import tree
from sklearn.tree import DecisionTreeClassifier
from sklearn.metrics import accuracy_score
model = DecisionTreeClassifier( #2
min_samples_leaf=40, criterion='entropy')
model.fit(Xtrn, ytrn) #3
ypred = model.predict(Xtst)
print('Accuracy = {0:4.3}%'.format(accuracy_score(ytst, ypred) * 100))
import graphviz, re, pydotplus
dot = tree.export_graphviz(model, feature_names=iris.feature_names,
class_names=['Iris-Setosa',
'Iris-Versicolour',
'Iris-Virginica'],
filled=True, impurity=False)
graphviz.Source(dot, format="png") #4
Train a scikit-learn decision tree, inspect its accuracy, and export its structure for interpretation. section 9.1.2

