Small rules, visible reasoning

Build a Decision Tree

Sort a basket of fruit using features you can see. Compare splits and discover why depth matters.

12 min to exploreMachine Learning · CPTR 235
01 / UNDERSTAND

Which question should the tree ask first?

A useful split makes the child groups more homogeneous. Compare candidate splits using weighted impurity, then decide how much complexity the evidence can support.

02 / EXPLORE

Make a prediction.
Then move a control.

03 / A tree you can read

Apple, orange, or banana?

Use color, shape, and size to classify 15 training fruits. Then evaluate on 9 separate examples. Every fruit and measurement is invented.

2
Depth 1 = one feature test from the root.
A small trap for the tree. Training fruit A4 is an apple deliberately mislabeled as an orange. A deeper tree may learn that error.
93.3%Training accuracy · observed labels14 / 15
100.0%Test accuracy · reference labels9 / 9
5Leaf rulesEach leaf predicts its majority class.

Increase the maximum depth to 3. Does a perfect training score improve the test result?

Shape = round → Color = redPrediction: AppleReference: Apple · correct

Why choose that first feature?

Compare the impurity before and after grouping by each feature. The reduction is the gain: larger means purer groups. This lab uses one branch for each categorical value.

FeatureBeforeWeighted afterGainEligible?

Inspect all training and test fruits

The tree sees the “Observed label” column for training. Test references are held separate from fitting. These 9 examples illustrate a mechanism; they are too small to establish general performance.

SetFruitColorShapeSizeReferenceObserved labelPrediction
Discuss

What evidence would justify a more complex tree? Compare depth 2 and 3, then remove the labeling error. Does every extra split help?

Illustrative categorical tree. This is a transparent teaching model, not an implementation of scikit-learn’s binary CART tree. Empty or unseen categories fall back to the node’s majority class; tied class counts use alphabetical order.

An educational experiment with illustrative data. The controls run locally in your browser.

03 / SEE THE CODE

The idea,
in Python.

A compact implementation of the core concept. Open the code when you are ready to connect the visual to the algorithm.

Open the core implementation Python ↗
Standalone example · illustrative valuesDownload .py
from collections import Counter, defaultdict

# Illustrative categorical fruit examples: the lab uses multiway splits.
fruits = [
    {"label": "apple", "color": "red", "shape": "round"},
    {"label": "apple", "color": "green", "shape": "round"},
    {"label": "orange", "color": "orange", "shape": "round"},
    {"label": "orange", "color": "yellow", "shape": "round"},
    {"label": "banana", "color": "yellow", "shape": "long"},
    {"label": "banana", "color": "green", "shape": "long"},
]

def gini(rows):
    return 1 - sum((n / len(rows)) ** 2
                   for n in Counter(r["label"] for r in rows).values())

for feature in ["color", "shape"]:
    groups = defaultdict(list)
    for row in fruits:
        groups[row[feature]].append(row)
    weighted = sum(len(g) / len(fruits) * gini(g) for g in groups.values())
    print(feature, "weighted impurity:", weighted,
          "reduction:", gini(fruits) - weighted)
04 / TRY IT

Explain the change.
Test your reasoning.

  1. Predict the best first split before revealing the impurity values.
  2. Change the feature used at the root. Which fruits remain mixed?
  3. Increase depth. Distinguish a clean training split from evidence of generalization.
← All visual labsNext: K-Means: Find the Groups

Find your next idea.

Search across courses, research, projects, and resources.

Ideas, connected.esc to close