Implementation:Online ml River Tree Nodes SGT
| Knowledge Sources | |
|---|---|
| Domains | Online_Learning, Decision_Trees, Gradient_Boosting |
| Last Updated | 2026-02-08 16:00 GMT |
Overview
Leaf node implementation for Stochastic Gradient Trees (SGT) that handles gradient and hessian information for gradient boosting.
Description
SGTLeaf is a specialized leaf node for Stochastic Gradient Trees that stores gradient and hessian statistics rather than raw target values. It uses feature quantizers (dynamic or static) to discretize numerical features and maintains split statistics for both categorical and numerical features. The leaf evaluates splits based on delta loss metrics and can update its prediction or split into branches based on gradient information.
Usage
Use SGTLeaf when building Stochastic Gradient Trees for regression or classification tasks using gradient boosting. The tree handles target transformation and encoding, while leaves manage gradient statistics.
Code Reference
Source Location
- Repository: Online_ml_River
- File: river/tree/nodes/sgt_nodes.py
Signature
class SGTLeaf(Leaf):
def __init__(self, prediction: float = 0.0, depth: int = 0, split_params: dict | None = None):
...
def update(self, x: dict, gh: GradHess, sgt, w: float = 1.0):
...
def prediction(self) -> float:
...
def find_best_split(self, sgt) -> BranchFactory:
...
def apply_split(self, split, p_node, p_branch, sgt):
...
@property
def total_weight(self) -> float:
...
@staticmethod
def delta_prediction(gh: GradHess, lambda_value: float):
...
Import
from river.tree.nodes.sgt_nodes import SGTLeaf
I/O Contract
| Input | Type | Description |
|---|---|---|
| prediction | float | Initial prediction value |
| depth | int | Node depth in tree |
| x | dict | Feature dictionary |
| gh | GradHess | Gradient and hessian pair |
| w | float | Sample weight |
| Output | Type | Description |
|---|---|---|
| prediction | float | Current prediction value |
| best_split | BranchFactory | Best split candidate including null split |
| delta_pred | float | Prediction update value |
Usage Examples
from river.tree.nodes.sgt_nodes import SGTLeaf
from river.tree.utils import GradHess
# Create SGT leaf
leaf = SGTLeaf(
prediction=0.5,
depth=1,
split_params={}
)
# Update with gradient/hessian
x = {'feature1': 0.5, 'feature2': 1.2}
gh = GradHess(gradient=-0.3, hessian=0.8)
leaf.update(x, gh, sgt=None, w=1.0)
# Get prediction
pred = leaf.prediction() # Returns 0.5
# Find best split
best_split = leaf.find_best_split(sgt=None)
# Calculate delta prediction
delta = SGTLeaf.delta_prediction(gh, lambda_value=1.0)