Implementation:Online ml River Tree Nodes Branch
| Knowledge Sources | |
|---|---|
| Domains | Online_Learning, Decision_Trees |
| Last Updated | 2026-02-08 16:00 GMT |
Overview
Branch node implementations for decision trees supporting binary and multiway splits on both numeric and nominal features.
Description
This module provides the branch (split) node implementations for decision trees. DTBranch is the base class, with four concrete implementations: NumericBinaryBranch (threshold-based splits on numerical features), NominalBinaryBranch (equality splits on categorical features), NumericMultiwayBranch (interval-based multiway splits using radius), and NominalMultiwayBranch (multiway splits on categorical values). Each branch type determines how to route samples to children and provides methods for finding the most common path.
Usage
Use these branch nodes when building decision tree structures. Binary branches are standard for most trees, while multiway branches can reduce tree depth but increase branching factor.
Code Reference
Source Location
- Repository: Online_ml_River
- File: river/tree/nodes/branch.py
Signature
class DTBranch(Branch):
def __init__(self, stats, *children, **attributes):
...
def branch_no(self, x):
pass
def next(self, x):
...
def max_branches(self):
pass
def repr_branch(self, index: int, shorten=False):
pass
class NumericBinaryBranch(DTBranch):
def __init__(self, stats, feature, threshold, depth, left, right, **attributes):
...
def branch_no(self, x):
return 0 if x[self.feature] <= self.threshold else 1
def most_common_path(self):
...
class NominalBinaryBranch(DTBranch):
def __init__(self, stats, feature, value, depth, left, right, **attributes):
...
def branch_no(self, x):
return 0 if x[self.feature] == self.value else 1
class NumericMultiwayBranch(DTBranch):
def __init__(self, stats, feature, radius_and_slots, depth, *children, **attributes):
...
def add_child(self, feature_val, child):
...
class NominalMultiwayBranch(DTBranch):
def __init__(self, stats, feature, feature_values, depth, *children, **attributes):
...
def add_child(self, feature_val, child):
...
Import
from river.tree.nodes.branch import NumericBinaryBranch
from river.tree.nodes.branch import NominalBinaryBranch
from river.tree.nodes.branch import NumericMultiwayBranch
from river.tree.nodes.branch import NominalMultiwayBranch
I/O Contract
| Input | Type | Description |
|---|---|---|
| stats | dict/Var | Split node statistics |
| feature | str/int | Feature name or index |
| threshold/value | float/any | Split threshold or categorical value |
| depth | int | Node depth |
| children | tuple | Child nodes |
| Output | Type | Description |
|---|---|---|
| branch_no | int | Child branch index for routing |
| next | Node | Next child node for given sample |
| repr_branch | str | String representation of branch test |
Usage Examples
from river.tree.nodes.branch import NumericBinaryBranch, NominalBinaryBranch
from river.tree.nodes.htc_nodes import LeafMajorityClass
from river.tree.splitter import GaussianSplitter
# Create leaves
left_leaf = LeafMajorityClass({'A': 10}, 2, GaussianSplitter())
right_leaf = LeafMajorityClass({'B': 8}, 2, GaussianSplitter())
# Create numeric binary branch
branch = NumericBinaryBranch(
stats={'A': 10, 'B': 8},
feature='age',
threshold=30.0,
depth=1,
left=left_leaf,
right=right_leaf
)
# Route sample
x = {'age': 25, 'income': 50000}
branch_idx = branch.branch_no(x) # Returns 0 (left)
next_node = branch.next(x) # Returns left_leaf
# Create nominal binary branch
nom_branch = NominalBinaryBranch(
stats={'A': 5, 'B': 5},
feature='color',
value='red',
depth=1,
left=left_leaf,
right=right_leaf
)
x2 = {'color': 'red'}
branch_idx = nom_branch.branch_no(x2) # Returns 0 (equal)