Decision Trees
Intermediate 12 min read Decision Trees

Interpretability and Visualization

A model you can read aloud

Trees tell their story: export_text, plotted trees, feature importances and decision paths make an entire model auditable in a printed page.

interpretabilitytree_export_textplot_treefeature importancedecision pathexplainability
Before we start

What you will learn

  • Render a tree with export_text and plot_tree
  • Read feature importances and their biases
  • Explain a single prediction via its decision path
Introduction

What is this?

Trees are natively interpretable: export_text renders the if-then rules as text; plot_tree draws the flowchart; feature_importances_ aggregates split gains; and tree_.decide_path reveals exactly which rock the prediction walked. All of it prints as plain artifacts for audits, decks and debugging — the reason regulated teams keep trees hands on.

Motivation

Why do you need it?

  • Audit: a printed tree is legal-grade documentation of a policy.
  • Debugging: the decision path of a misfit reveals the precise condition that misclassified it.
  • Feature story: importances answer 'what drives this decision' in one chart.
  • Communication: explain the model to a VP in minutes, not a thesis.
Think about it

Mental model

Imagine

A recipe card that states exact steps vs a chef's instinct

Think of

Tree interpretability

You can hand the card to an intern and they reproduce the dish; an unviolable 'chef's instinct' (a deep net) leaves no card to review.

Mechanics

How it works

  • export_text: print the tree as indented if-then rules (max_depth controls verbosity).
  • plot_tree: render the splits/leaves graphically; set filled=True for class coloring.
  • feature_importances_: sum of normalized gain each feature contributed across splits.
  • Decision path: new row -> exact node sequence; show the guards that decide it.
  • Combine with SHAP when stakeholders need per-feature magnitude on a tree (they compute from the same split structure).

Explainability artifacts.

Rules
Picture
Importance
Path
Hands-on

Examples

Render, read, explain one row

Render, read, explain one row
from sklearn.tree import DecisionTreeClassifier, export_text
from sklearn import tree
from sklearn.datasets import load_iris
import numpy as np
d = load_iris()
m = DecisionTreeClassifier(max_depth=3, random_state=0).fit(d.data, d.target)
print(export_text(m, feature_names=d.feature_names, max_depth=3))
sample = d.data[17:18]
path = m.decision_path(sample).toarray()[0]
node = int(np.nonzero(path)[0][-1])
left = m.tree_.children_left[node]; right = m.tree_.children_right[node]
print("leaf node:", node, "->", m.tree_.feature[node],
"splits further:", left != -1 or right != -1)
import matplotlib
matplotlib.use("Agg")
import matplotlib.pyplot as plt
fig = tree.plot_tree(m, filled=True)
plt.savefig("/tmp/iris_tree.png", dpi=100)
print("saved /tmp/iris_tree.png")

The text rules, the printed plot, the decision-path and importance all speak the same auditable language — the whole model on one page.

Real world

In production

  • Model approval gates: export_text + plot are the standard packet.
  • Support escalations: 'this customer was rejected because petal width <= 0.8...' is answerable with a decision path.
  • Policy drift review: diffs of export_text across retrains show exactly what changed.
Guidelines

Best practices

  • Keep audited trees shallow enough to print; store the export_text and an importance chart in the model card; quote importances with the caveat that collinear features split the credit arbitrarily.
Watch out

Common mistakes

  • Believing importances are 'effect sizes' (they're partition-gain shares, biased toward high-cardinality/collinear features); printing depth-20 trees nobody can read; ignoring the decision path when the model misfits.
When things break

Debugging tips

  • A misfit row -> print its decision path; the failing condition reads like a data bug.
  • Importance lists one engineered feature as #1 suspiciously often => check for collinearity.
  • Deep tree, unreadable plot => shrink depth/leaves before presenting.
Interviews

Interview checkpoint

Test yourself

Quick quiz

0/2
1

export_text renders a tree as:

2

A regularized 'haunted' importance list is often caused by:

Go deeper

Official documentation