MODELCATALOG

A single decision tree. Readable, and prone to overfitting.

sklearn.tree

tree

A single decision tree. Readable, and prone to overfitting.

1 Get data Something to practise on
2 Split & tune An honest test set
3 Prepare Into numbers a model accepts
4 Compose One object, no leakage
5 Model The algorithm itself
6 Unsupervised No labels available
7 Measure Is it any good, and why
8 Utilities Glue and extension

What it is for

Rarely the right production model alone, but essential for teaching and for the occasional case where a fully readable rule set is a hard requirement.

Contents

DecisionTreeClassifier / Regressor One tree. Constrain max_depth or it memorises the training set
export_text common Print the actual rules as text
plot_tree Draw it

Worked examples 1

Runnable, with the arguments that change the outcome called out. Output shapes are shown as comments where they help.

Print the actual rules

The one thing a single tree does that nothing else does: give you a decision procedure a human can read and audit line by line.

from sklearn.tree import DecisionTreeClassifier, export_text

tree = DecisionTreeClassifier(
    max_depth=3,              # constrain it or it memorises the data
    min_samples_leaf=100,     # no leaf decided by a handful of rows
    class_weight="balanced",
    random_state=42,
)
tree.fit(X_train, y_train)

print(export_text(tree, feature_names=list(feature_names),
                  max_depth=3, decimals=1))

# |--- months_delinquent <= 1.5
# |   |--- income <= 28500.0
# |   |   |--- class: 1
# |   |--- income >  28500.0
# |   |   |--- class: 0
# |--- months_delinquent >  1.5
# |   |--- class: 1
#
# A shallow tree is often worth fitting purely as documentation, even
# when you deploy a boosted ensemble - it tells you what the dominant
# splits are in language a committee understands.