Skip to content
C.W.K.
Stream
Lesson 05 of 07 · published

Decision Trees

~28 min · trees, interpretable

Level 0Scout
0 XP0/48 lessons0/11 achievements
0/120 XP to next level120 XP to go0% complete

A decision tree partitions space with questions

A tree repeatedly asks a threshold question about one feature and chooses the split that most reduces impurity. Classification commonly uses Gini impurity or entropy; regression commonly reduces squared or absolute error. A leaf predicts a class distribution or the average target of the training rows that reach it.

Nonlinearity and interactions come naturally

Several splits can represent demand that rises at both low and high temperatures, or a different age threshold by region. Trees use numeric ordering rather than feature magnitude, so they do not require scaling or a normality assumption.

Categorical and missing-value support is implementation-specific

Some tree libraries handle categories and missing values directly. Scikit-learn's basic tree estimators generally expect numeric input and preprocessing, while CatBoost and LightGBM have their own strategies. Inspect the chosen library's contract and test unseen-category behavior instead of assuming every tree accepts mixed raw columns.

Splitting is greedy

Each step chooses the best immediate impurity reduction and does not revisit earlier choices to find a globally optimal tree. On a small sample, a locally attractive split may be accidental, and an unconstrained tree can continue until tiny leaves memorize training noise.

Small data changes can change the whole tree

When two candidate splits are close, moving a few rows may change the first question and every branch below it. Evaluate a single tree across folds or seeds and inspect whether important upper splits are stable, not only whether one diagram looks convincing.

Control complexity directly

Tune max_depth, min_samples_leaf, min_samples_split, or cost-complexity pruning with validation data. Require enough support in displayed leaves; a readable rule backed by three rows is weak evidence. A depth-three to depth-five tree can still be valuable as an inspectable baseline.

An explanation is not a causal account

A path shows how this fitted model produced a prediction. It does not prove that the first split caused the outcome, and correlated features may substitute for one another. Use surprising rules as questions for source-data and domain review rather than declaring them business truth.

Move to ensembles for predictive stability

Compare a shallow tree with a linear baseline, random forest, and gradient boosting on the same untouched split. Forests and boosting usually reduce the variance of one deep tree. Keep the shallow tree as documentation only when metrics and leaf support justify its story, and accept ensemble complexity only when the improvement is meaningful.

Code

Train and visualize a shallow tree·python
from sklearn.tree import DecisionTreeClassifier, export_text

tree = DecisionTreeClassifier(max_depth=4, class_weight="balanced", random_state=7)
tree.fit(X_train, y_train)
print(export_text(tree, feature_names=list(X_train.columns)))
Plot the tree in a notebook·python
from sklearn.tree import plot_tree
import matplotlib.pyplot as plt

plt.figure(figsize=(14, 7))
plot_tree(tree, filled=True, feature_names=list(X_train.columns), max_depth=3)
plt.show()

External links

Exercise

Train a max_depth=4 decision tree on your dataset. Print it with export_text. Show it to a non-technical stakeholder and ask if the splits match their domain intuition. Note any surprises as data hypotheses to investigate.

Progress

Progress is local-only — sign in to sync across devices.
Spotted a bug or have feedback on this page?Report an Issue

Comments 0

🔔 Reply notifications (sign in)
Sign inPlease sign in to comment.

No comments yet — be the first.