Implementation:Online ml River Tree Nodes HTLeaf
| Knowledge Sources | |
|---|---|
| Domains | Online_Learning, Decision_Trees |
| Last Updated | 2026-02-08 16:00 GMT |
Overview
Base abstract class for all leaf nodes in Hoeffding Trees, defining the core interface and functionality for learning nodes.
Description
HTLeaf is the foundational abstract class for all Hoeffding Tree leaf nodes. It manages target statistics, depth, attribute splitters, and provides methods for updating statistics, finding split candidates, and making predictions. Leaves can be activated or deactivated to manage memory. The class enforces implementation of abstract methods for total_weight calculation, prediction generation, promise calculation, and statistics updates.
Usage
Do not instantiate HTLeaf directly. Instead, extend it to create specific leaf node types for classification or regression tasks with different prediction strategies.
Code Reference
Source Location
- Repository: Online_ml_River
- File: river/tree/nodes/leaf.py
Signature
class HTLeaf(Leaf, abc.ABC):
def __init__(self, stats, depth, splitter, **kwargs):
...
@property
@abc.abstractmethod
def total_weight(self) -> float:
pass
def is_active(self):
...
def activate(self):
...
def deactivate(self):
...
@staticmethod
@abc.abstractmethod
def new_nominal_splitter():
pass
@abc.abstractmethod
def update_stats(self, y, w):
pass
def update_splitters(self, x, y, w, nominal_attributes):
...
def best_split_suggestions(self, criterion, tree) -> list[BranchFactory]:
...
def disable_attribute(self, att_id):
...
def learn_one(self, x, y, *, w=1.0, tree=None):
...
@abc.abstractmethod
def prediction(self, x, *, tree=None) -> dict:
pass
@abc.abstractmethod
def calculate_promise(self) -> int:
pass
Import
from river.tree.nodes.leaf import HTLeaf
I/O Contract
| Input | Type | Description |
|---|---|---|
| stats | dict/Var | Target statistics (dict for classification, Var for regression) |
| depth | int | Node depth in tree |
| splitter | Splitter | Numeric attribute observer |
| x | dict | Feature dictionary |
| y | Target | Target value (class label or numeric) |
| w | float | Sample weight |
| Output | Type | Description |
|---|---|---|
| split_suggestions | list[BranchFactory] | List of candidate splits |
| prediction | dict/float | Prediction (probabilities for classification, value for regression) |
| promise | int | Node promise for splitting |
Usage Examples
# HTLeaf is abstract, so use a concrete implementation
from river.tree.nodes.htc_nodes import LeafMajorityClass
from river.tree.splitter import GaussianSplitter
leaf = LeafMajorityClass(
stats={'A': 10, 'B': 5},
depth=1,
splitter=GaussianSplitter()
)
# Check if active
assert leaf.is_active()
# Update with sample
x = {'feature1': 0.5}
leaf.learn_one(x, y='A', w=1.0, tree=None)
# Deactivate to save memory
leaf.deactivate()
assert not leaf.is_active()
# Get split suggestions
from river.tree.split_criterion import GiniSplitCriterion
criterion = GiniSplitCriterion()
suggestions = leaf.best_split_suggestions(criterion, tree=None)