Chapter DDecision treesPage 3 of 8

Decision trees

Implement the happy path

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

~14 minHappy path

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.

1Learn the idea

Read

Build the chapter step

Build the complete path once without adding optional features. Enter the example exactly, predict the expected output, run it, and compare. Then change one meaningful value connected to study hours and practice-test count predict readiness, and each prediction must have a traceable path and explain why the result should change.

The deliverable for this step is a depth-limited classification tree with held-out accuracy and printed decision rules.

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

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.

For the current build 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.

Keep the example small enough to inspect manually. Small does not mean careless: boundary values, file locations, feature order, and held-out data still determine whether the result means what you claim.

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 text categories in numeric columns require consistent encoding rather than arbitrary integer labels?
  3. Can a new user reproduce a depth-limited classification tree with held-out accuracy and printed decision rules from the stated setup?