Implementation:Online ml River Metrics MultiLabelConfusionMatrix
| Knowledge Sources | |
|---|---|
| Domains | Online_Learning, Evaluation_Metrics, Multi_Label_Classification |
| Last Updated | 2026-02-08 16:00 GMT |
Overview
Multi-label confusion matrix maintaining separate binary confusion matrices for each output label.
Description
MultiLabelConfusionMatrix stores one confusion matrix per output label in multi-label classification tasks. It accepts predictions and true labels as dictionaries mapping label indices to boolean values. Under the hood, it maintains a dictionary of metrics.ConfusionMatrix instances, one for each label, allowing detailed per-label performance analysis in multi-label scenarios where samples can belong to multiple classes simultaneously.
Usage
Use MultiLabelConfusionMatrix when evaluating multi-label classifiers and you need detailed per-label confusion matrices. This is essential for understanding which labels are being confused, identifying problematic labels, and computing per-label metrics like precision, recall, and F1. Each label's confusion matrix can be accessed independently for targeted analysis.
Code Reference
Source Location
- Repository: Online_ml_River
- File: river/metrics/multioutput/confusion.py
Signature
class MultiLabelConfusionMatrix:
def __init__(self):
pass
def update(self, y_true, y_pred, w=1.0):
# y_true, y_pred: dict mapping labels to boolean values
pass
def revert(self, y_true, y_pred, w=1.0):
pass
Import
from river import metrics
I/O Contract
| Method | Parameters | Returns | Description |
|---|---|---|---|
| update | y_true (dict), y_pred (dict), [w] | None | Updates confusion matrices for all labels |
| revert | y_true (dict), y_pred (dict), [w] | None | Reverts confusion matrices for all labels |
| __repr__ | - | str | Returns formatted display of all label confusion matrices |
Usage Examples
from river import metrics
cm = metrics.multioutput.MultiLabelConfusionMatrix()
y_true = [
{0: False, 1: True, 2: True},
{0: True, 1: True, 2: False}
]
y_pred = [
{0: True, 1: True, 2: True},
{0: True, 1: False, 2: False}
]
for yt, yp in zip(y_true, y_pred):
cm.update(yt, yp)
print(cm)
# 0
# False True
# False 0 1
# True 0 1
#
# 1
# False True
# False 0 0
# True 1 1
#
# 2
# False True
# False 1 0
# True 0 1
# Access individual label confusion matrices
print(f"Label 0 confusion matrix: {cm.data[0]}")
print(f"Label 1 true positives: {cm.data[1].true_positives(True)}")
# Useful for computing per-label metrics
for label in cm.data:
tp = cm.data[label].true_positives(True)
fp = cm.data[label].false_positives(True)
fn = cm.data[label].false_negatives(True)
precision = tp / (tp + fp) if (tp + fp) > 0 else 0
recall = tp / (tp + fn) if (tp + fn) > 0 else 0
print(f"Label {label}: Precision={precision:.2f}, Recall={recall:.2f}")