Lesson 18: Decision Trees — AI That Explains Itself

Explain how decision trees make predictions; visualize a decision tree.

By • Last updated • 70 min

Decision Trees: AI That Explains Itself

Imagine playing a game of 20 Questions. You try to guess what someone is thinking by asking yes/no questions that divide the possibilities in half. A Decision Tree algorithm does exactly this!

Splitting the Data

A decision tree examines your data and finds the best questions to ask. At each step (or "node"), it splits the data based on a feature (e.g., "Is age > 30?"). It keeps splitting until the resulting groups are as "pure" as possible (meaning they mostly contain one category).

Decision Tree Splitting Visualizer

Configure splits and see how a Decision Tree recursively partitions a 2D feature space.

Apples and Grapes are clearly separated on the left and right. A single vertical split at X = 5.0 separates them perfectly.
Max Depth (max_depth)1
Depth 1 (1 Split)Depth 2 (3 Splits)Depth 3 (7 Splits)
Dataset Purity Breakdown:
Apples: 5
Grapes: 5
2D Feature Space (Fruit Classification)
00224466881010Fruit Size (Feature X)Sweetness (Feature Y)
Interactive Decision Tree Structure
Root Node
📏 Size< 5.0
n=10 | G=0.50
Size < 5.0
🍏 Apple
n=5 | G=0.00
Size ≥ 5.0
🍇 Grape
n=5 | G=0.00
Selected Split Node
Node ID: 0 (Depth 0)

This is the root node where all training samples enter. Adjusting the root threshold splits the entire feature space.

Split Threshold5.0
Step-by-Step Splitting Mathematics
Gini Impurity (For current node)
Gini = 1 - P(Apple)² - P(Grape)²
P(Apple) = 5 / 10 = 0.500
P(Grape) = 5 / 10 = 0.500
Gini = 1 - (0.500)² - (0.500)²
Gini = 0.500
Information Gain of Split
Gain = Gini_Parent - [ (N_L/N)*Gini_L + (N_R/N)*Gini_R ]
Left: 5 samples (Gini = 0.000)
Right: 5 samples (Gini = 0.000)
Gain = 0.500 - [ (0.50 * 0.000) + (0.50 * 0.000) ]
Gain = 0.500

This split successfully reduces the impurity of the subregions by 0.500. The algorithm aims to find splits that maximize this Gain.

The biggest danger with decision trees is Overfitting. If you let it ask infinite questions, it will memorize every single person in the training set instead of learning general patterns. We can control this by setting a maximum depth (`max_depth`).

Python Challenge: Plant a Tree

Train a Decision Tree Classifier and try changing the max_depth parameter.

from sklearn.tree import DecisionTreeClassifier
from sklearn.datasets import load_iris
from sklearn.model_selection import train_test_split
from sklearn.metrics import accuracy_score

X, y = load_iris(return_X_y=True)
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)

# TODO: Initialize DecisionTreeClassifier with max_depth=2
# tree = ???

# TODO: Fit the model and check test accuracy
# tree.???
# preds = tree.predict(X_test)
# print(f"Accuracy: {accuracy_score(y_test, preds)}")

Unlike deep neural networks which are "black boxes", decision trees are highly interpretable. You can print the tree and see exactly what logic it used to make a prediction!

main.py
Run your code to see output here.