Implementation:Online ml River Tree Nodes HATC
| Knowledge Sources | |
|---|---|
| Domains | Online_Learning, Decision_Trees |
| Last Updated | 2026-02-08 16:00 GMT |
Overview
Hoeffding Adaptive Tree Classifier (HATC) node implementations with drift detection and alternate subtree management for adapting to concept drift.
Description
This module implements adaptive nodes for Hoeffding Adaptive Tree classifiers. The AdaLeafClassifier uses Naive Bayes Adaptive prediction with ADWIN drift detection to monitor classification errors. The AdaBranchClassifier maintains alternate subtrees that are grown when drift is detected and can replace the main subtree if they perform better. Bootstrap sampling via Poisson distribution is supported for improved learning.
Usage
Use HATC nodes when building classification trees that need to adapt to concept drift in non-stationary data streams. The drift detection mechanism automatically creates alternate subtrees and switches to them when beneficial.
Code Reference
Source Location
- Repository: Online_ml_River
- File: river/tree/nodes/hatc_nodes.py
Signature
class AdaLeafClassifier(LeafNaiveBayesAdaptive):
def __init__(self, stats, depth, splitter, drift_detector, rng, **kwargs):
...
def learn_one(self, x, y, *, w=1.0, tree=None, parent=None, parent_branch=None):
...
def prediction(self, x, *, tree=None):
...
class AdaBranchClassifier(DTBranch):
def __init__(self, stats: dict, *children, drift_detector: base.DriftDetector, **attributes):
...
def traverse(self, x, until_leaf=True) -> list[HTLeaf]:
...
def learn_one(self, x, y, *, w=1.0, tree=None, parent=None, parent_branch=None):
...
def kill_tree_children(self, tree):
...
Import
from river.tree.nodes.hatc_nodes import AdaLeafClassifier
from river.tree.nodes.hatc_nodes import AdaBranchClassifier
I/O Contract
| Input | Type | Description |
|---|---|---|
| x | dict | Feature dictionary |
| y | int/str | Target class label |
| w | float | Sample weight (default 1.0) |
| drift_detector | DriftDetector | Drift detection algorithm instance |
| Output | Type | Description |
|---|---|---|
| prediction | dict | Class probabilities weighted by error estimates |
| found_nodes | list[HTLeaf] | Leaves including alternate subtree leaves |
Usage Examples
from river.tree.nodes.hatc_nodes import AdaLeafClassifier
from river.drift import ADWIN
from river.tree.splitter import GaussianSplitter
import random
# Create adaptive leaf with drift detection
rng = random.Random(42)
leaf = AdaLeafClassifier(
stats={0: 5, 1: 5},
depth=1,
splitter=GaussianSplitter(),
drift_detector=ADWIN(),
rng=rng
)
# Update with samples (uses Poisson bootstrap)
x = {'feature1': 0.5, 'feature2': 1.2}
leaf.learn_one(x, y=1, w=1.0, tree=None)
# Predictions are weighted by error estimates
pred = leaf.prediction(x, tree=None)