Decision Trees

A tree of yes/no questions that splits data toward a prediction.

SyntaxDecisionTreeClassifier(max_depth=3)

A decision tree asks a sequence of yes/no questions about the features, splitting the data until it reaches a prediction at a leaf.

Why people love them

  • Interpretable — you can read the rules.
  • No scaling needed; handle numeric and categorical data.
  • But a deep tree easily overfits — limit its depth.

Example

Example · python
from sklearn.tree import DecisionTreeClassifier
import numpy as np

X = np.array([[1], [2], [3], [6], [7], [8]])
y = np.array([0, 0, 0, 1, 1, 1])

model = DecisionTreeClassifier(max_depth=2, random_state=0)
model.fit(X, y)

print(model.predict([[4]]))  # [0]
print(model.predict([[7]]))  # [1]

When to use it

  • A credit analyst uses a decision tree to create an interpretable loan approval model whose rules can be explained to regulators.
  • A healthcare team uses a shallow decision tree (max_depth=3) as a diagnostic triage tool that nurses can follow without a computer.
  • An engineer uses feature_importances_ from a decision tree to rank which sensor readings most strongly predict machine failures.

More examples

Train and score a decision tree

Trains a depth-limited decision tree on the Iris dataset and reports its accuracy on the held-out test set.

Example · python
from sklearn.tree import DecisionTreeClassifier
from sklearn.datasets import load_iris
from sklearn.model_selection import train_test_split

X, y = load_iris(return_X_y=True)
X_tr, X_te, y_tr, y_te = train_test_split(X, y, test_size=0.2, random_state=0)

tree = DecisionTreeClassifier(max_depth=3, random_state=0).fit(X_tr, y_tr)
print('Accuracy:', tree.score(X_te, y_te).round(3))

Inspect feature importances

Reads the feature_importances_ attribute to see which input features the tree used most, providing a simple built-in explanation.

Example · python
from sklearn.tree import DecisionTreeClassifier
from sklearn.datasets import load_iris
import pandas as pd

X, y = load_iris(return_X_y=True)
features = load_iris().feature_names
tree = DecisionTreeClassifier(max_depth=4).fit(X, y)

imp = pd.Series(tree.feature_importances_, index=features).sort_values(ascending=False)
print(imp)

Visualise the tree structure

Exports the fitted tree as a human-readable text showing each split condition and leaf prediction, making the model fully interpretable.

Example · python
from sklearn.tree import DecisionTreeClassifier, export_text
from sklearn.datasets import load_iris

X, y = load_iris(return_X_y=True)
tree = DecisionTreeClassifier(max_depth=2).fit(X, y)

rules = export_text(tree, feature_names=list(load_iris().feature_names))
print(rules)

Discussion

  • Be the first to comment on this lesson.