Implementation:Online ml River Metrics FBeta
| Knowledge Sources | |
|---|---|
| Domains | Online_Learning, Evaluation_Metrics |
| Last Updated | 2026-02-08 16:00 GMT |
Overview
F-Beta score family of metrics for binary and multi-class classification with configurable beta weighting.
Description
This module provides the F-Beta score, a weighted harmonic mean between precision and recall where beta controls the importance of recall. It includes FBeta (binary), F1 (binary with beta=1), MacroFBeta, MicroFBeta, WeightedFBeta, MultiFBeta (different betas per class), and their F1 variants (MacroF1, MicroF1, WeightedF1). Higher beta values weight recall more heavily than precision.
Usage
Use F-Beta metrics when you need to balance precision and recall with custom weighting. F1 (beta=1) gives equal weight to both. Increase beta to prioritize recall (important when false negatives are costly), or decrease beta to prioritize precision (when false positives are costly). Macro, micro, and weighted variants handle multi-class scenarios differently.
Code Reference
Source Location
- Repository: Online_ml_River
- File: river/metrics/fbeta.py
Signature
class FBeta(metrics.base.BinaryMetric):
def __init__(self, beta: float, cm=None, pos_val=True):
pass
class F1(FBeta):
def __init__(self, cm=None, pos_val=True):
super().__init__(beta=1.0, cm=cm, pos_val=pos_val)
class MacroFBeta(metrics.base.MultiClassMetric):
def __init__(self, beta, cm=None):
pass
class MicroFBeta(metrics.base.MultiClassMetric):
def __init__(self, beta: float, cm=None):
pass
class WeightedFBeta(metrics.base.MultiClassMetric):
def __init__(self, beta, cm=None):
pass
class MultiFBeta(metrics.base.MultiClassMetric):
def __init__(self, betas, weights, cm=None):
pass
Import
from river import metrics
I/O Contract
| Method | Parameters | Returns | Description |
|---|---|---|---|
| update | y_true, y_pred, [w] | None | Updates metric with true and predicted labels |
| get | - | float | Returns F-Beta score (0.0 to 1.0) |
Usage Examples
from river import metrics
# Binary FBeta with beta=2 (recall weighted twice as much as precision)
y_true = [False, False, False, True, True, True]
y_pred = [False, False, True, True, False, False]
metric = metrics.FBeta(beta=2)
for yt, yp in zip(y_true, y_pred):
metric.update(yt, yp)
print(metric)
# FBeta: 35.71%
# Binary F1 score (beta=1, equal weighting)
metric_f1 = metrics.F1()
for yt, yp in zip(y_true, y_pred):
metric_f1.update(yt, yp)
print(metric_f1)
# F1: 40.00%
# Multi-class Macro F-Beta (average per-class F-Beta scores)
y_true = [0, 1, 2, 2, 2]
y_pred = [0, 0, 2, 2, 1]
metric_macro = metrics.MacroFBeta(beta=0.8)
for yt, yp in zip(y_true, y_pred):
metric_macro.update(yt, yp)
print(metric_macro)
# MacroFBeta: 48.60%
# Multi-class with different beta per class
metric_multi = metrics.MultiFBeta(
betas={0: 0.25, 1: 1, 2: 4},
weights={0: 1, 1: 1, 2: 2}
)
y_true = [0, 1, 2, 2, 2]
y_pred = [0, 0, 2, 2, 1]
for yt, yp in zip(y_true, y_pred):
metric_multi.update(yt, yp)
print(metric_multi)
# MultiFBeta: 46.88%