Implementation:Online ml River Metrics BalancedAccuracy
| Knowledge Sources | |
|---|---|
| Domains | Online_Learning, Evaluation_Metrics |
| Last Updated | 2026-02-08 16:00 GMT |
Overview
Balanced accuracy metric that averages recall across all classes to handle imbalanced datasets.
Description
BalancedAccuracy computes the average of recall obtained on each class, providing a more reliable measure than regular accuracy for imbalanced datasets. It works for both binary and multi-class classification problems by computing the arithmetic mean of class-wise recall values, giving equal importance to each class regardless of its frequency.
Usage
Use BalancedAccuracy when evaluating classifiers on imbalanced datasets where some classes are underrepresented. Unlike regular accuracy which can be misleading when classes are imbalanced, balanced accuracy ensures each class contributes equally to the final score, making it suitable for scenarios where all classes are equally important.
Code Reference
Source Location
- Repository: Online_ml_River
- File: river/metrics/balanced_accuracy.py
Signature
class BalancedAccuracy(metrics.base.MultiClassMetric):
def __init__(self, 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 balanced accuracy score (0.0 to 1.0) |
Usage Examples
from river import metrics
# Binary classification example
y_true = [True, False, True, True, False, True]
y_pred = [True, False, True, True, True, False]
metric = metrics.BalancedAccuracy()
for yt, yp in zip(y_true, y_pred):
metric.update(yt, yp)
print(metric)
# BalancedAccuracy: 62.50%
# Multi-class example
y_true = [0, 1, 0, 0, 1, 0]
y_pred = [0, 1, 0, 0, 0, 1]
metric = metrics.BalancedAccuracy()
for yt, yp in zip(y_true, y_pred):
metric.update(yt, yp)
print(metric)
# BalancedAccuracy: 62.50%
# The metric averages recall per class:
# Class 0: 3/4 correct = 75%
# Class 1: 1/2 correct = 50%
# Balanced Accuracy: (75% + 50%) / 2 = 62.5%