liveBook · Manning

Decision node

As explained in Grokking AI Algorithms, Second Edition

A decision node is an internal node of a decision tree that contains a test used to partition examples.

A decision node is a question inside a decision tree. Like a fork in a path, it sends examples one way when the answer is true and another way when it is false. A leaf node ends the path with a classification. section 8.17

Sorting diamonds by cut

Suppose the goal is to classify diamonds as Okay or Perfect using their carat and price. Ask, “Is Carat ≥ 1.13?” In the sample data, all four diamonds answering true are Perfect. The false branch still contains both labels, so it needs another question. “Is Carat ≥ 0.76?” separates three Okay diamonds from the smaller diamonds, which need further sorting. These are illustrative questions, not necessarily the questions a training algorithm would choose. section 8.16 section 8.17

To build the tree, try possible questions at each point and compare how much they reduce uncertainty. A question combines a feature, a value, and—in this simple implementation—a ≥ comparison. The selected decision node holds that question and two branches; the process repeats on each branch until it creates leaves. Constraints matter because a tree can memorize its training examples instead of finding patterns that generalize. section 8.17 section 8.16

class ExamplesNode: #A
   def __init__(self, examples):
       self.predictions = find_unique_label_counts(examples)

   def __repr__(self):
       return f"Leaf: {self.predictions}"

class DecisionNode: #B
   def __init__(self, question, true_branch, false_branch):
       self.question = question
       self.true_branch = true_branch
       self.false_branch = false_branch

   def __repr__(self):
       return f"DecisionNode: {self.question}"

def build_tree(examples, feature_names):
   gain, question = find_best_split(examples, feature_names)

   if gain == 0:
       return ExamplesNode(examples) #C

   true_examples, false_examples = split_examples(examples, question) #D

   true_branch = build_tree(true_examples, feature_names) #E
   false_branch = build_tree(false_examples, feature_names) #E

   return DecisionNode(question, true_branch, false_branch)

The Python listing represents a decision node with a question, a true branch, and a false branch. Its tree-building function splits examples, builds both branches recursively, and returns a leaf when it finds no gain. section 8.17

How Grokking AI Algorithms, Second Edition covers it

How deep it goes
Defines node contents and walks through question testing, splitting, and recursive construction; notes overfitting without detailing constraints.
Examples
Uses Okay and Perfect diamond labels, carat and price thresholds, and a Python decision-node class.
What it assumes
Assumes familiarity with basic Python classes and collections while explaining decision questions and uncertainty during training.
What you can do after
Build it from scratch as a Python node with a question and recursively constructed true and false branches.
How it connects
Connects decision trees and leaf nodes to feature-threshold questions, Gini uncertainty, and information gain.

Also explained in

Compare how 3 books explain decision node →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Grokking AI Algorithms, Second Edition. The section links point to the parts of the book it draws on.