Chapter DDecision treesPage 1 of 8

Decision trees

Define the lab goal and success criteria

A decision tree predicts by testing one feature threshold at each node and following branches until a leaf returns a class.

~14 minLab goal

1Try it yourself

Playground

Choose the first split

Pick the rule a decision tree should use at a branch.

Before you start

Why this matters

Before running anything, predict one observable result from the case: study hours and practice-test count predict readiness, and each prediction must have a traceable path. Write the prediction beside the command or code line that should cause it. This makes the session an experiment rather than a transcription exercise.

2Learn the idea

Read

Explain the chapter step

Begin by writing the success condition in observable terms. For this case, success is not familiarity with the vocabulary; it is producing a depth-limited classification tree with held-out accuracy and printed decision rules. Record the starting state so you can distinguish an improvement from a result that was already present.

On this page, the practical job is to state a measurable outcome before changing anything. The running case is study hours and practice-test count predict readiness, and each prediction must have a traceable path.

Read

Run the working example

from sklearn.tree import DecisionTreeClassifier, export_text

X = [[1, 0], [2, 1], [4, 1], [5, 3], [6, 2], [7, 4]]
y = ["needs-practice", "needs-practice", "almost", "ready", "ready", "ready"]

tree = DecisionTreeClassifier(max_depth=2, min_samples_leaf=1, random_state=7)
tree.fit(X[:5], y[:5])
print(tree.predict([[7, 4]])[0])
print(export_text(tree, feature_names=["hours", "practice_tests"]))

Expected evidence:

ready
|--- hours <= 3.00
|   |--- class: needs-practice
|--- hours >  3.00
|   |--- hours <= 4.50
|   |   |--- class: almost
|   |--- hours >  4.50
|   |   |--- class: ready

The output may include version-specific details such as hashes, paths, fitted thresholds, or final decimal places. Compare the structural facts described here rather than copying placeholders. If the structure differs, stop and inspect the earliest unexpected line.

Read

Read it line by line

  1. the classifier receives two features in a fixed positional order.
  2. max_depth=2 limits the number of learned questions along a path.
  3. fit searches splits using only the first five rows.
  4. the final row is held out; export_text exposes the learned thresholds instead of relying on a diagram.

These lines form one chain: numeric rows [hours, practice_tests] with readiness labels becomes class predictions, a readable tree, and held-out accuracy. Change only one input first. When several values change together, you cannot tell which change caused the new behavior.

Read

Common errors and fixes

  • First failure: reversing feature order at prediction time silently changes the question each column answers. Re-run the smallest command that proves the repair.
  • Second failure: an unconstrained tree can create leaves containing one training row and memorize noise. Preserve the failing input as a test when it represents a realistic mistake.
  • Misleading success: text categories in numeric columns require consistent encoding rather than arbitrary integer labels. A clean-looking final line cannot cancel contradictory intermediate evidence.

When debugging, copy the exact error text and inspect names, paths, shapes, types, and versions. Explain the cause in one sentence before changing code. That discipline prevents a guessed repair from creating a second defect.

Read

Evidence for this stage

Evaluate several held-out rows with a confusion matrix, compare training and test accuracy, and inspect leaf sample counts. A large training-test gap or tiny leaves indicates overfitting.

For the current explain step, save the smallest useful evidence: the relevant command, its output, and the input that produced it. Do not use a screenshot as the only record when text can be copied and searched. Keep generated artifacts separate from source inputs so rerunning the example does not destroy the evidence it is meant to evaluate.

Publish the depth, leaf constraints, feature order, class meanings, held-out metrics, and a text or visual representation. State that paths explain model mechanics, not causation.

Read

Reflect on the result

Return to your opening prediction. Mark it correct or rewrite it with the condition you missed. Then explain the difference between a successful execution and a trustworthy result for this specific example.

Checking tutor…

Continue learning · glossary & guides
  1. Which line or command establishes the current step's most important fact?
  2. What output would reveal that reversing feature order at prediction time silently changes the question each column answers?
  3. Can a new user reproduce a depth-limited classification tree with held-out accuracy and printed decision rules from the stated setup?