Implementation:Online ml River Metrics ConfusionMatrix
| Knowledge Sources | |
|---|---|
| Domains | Online_Learning, Evaluation_Metrics |
| Last Updated | 2026-02-08 16:00 GMT |
Overview
Confusion matrix for binary and multi-class classification tracking true/false positives/negatives.
Description
ConfusionMatrix maintains a 2D matrix where rows represent true class labels and columns represent predicted class labels. It incrementally tracks all classifications, providing access to counts via indexing (cm[true_label][pred_label]) and computing derived statistics like true positives, false positives, true negatives, and false negatives for each class. The matrix updates online as new predictions arrive.
Usage
Use ConfusionMatrix as a foundational metric for classification evaluation. It serves as the underlying storage for most classification metrics in River. Access individual cell counts with bracket notation, retrieve class-wise statistics with methods like true_positives(), false_positives(), or pass it to other metrics via the cm parameter to share computation and reduce memory overhead.
Code Reference
Source Location
- Repository: Online_ml_River
- File: river/metrics/confusion.py
Signature
class ConfusionMatrix(metrics.base.MultiClassMetric):
def __init__(self, classes=None):
pass
def __getitem__(self, key):
"""Access confusion matrix counts via cm[true_label][pred_label]"""
pass
def support(self, label):
"""Number of times label appears in y_true"""
pass
def true_positives(self, label):
"""True positives for a given label"""
pass
def false_positives(self, label):
"""False positives for a given label"""
pass
def false_negatives(self, label):
"""False negatives for a given label"""
pass
def true_negatives(self, label):
"""True negatives for a given label"""
pass
Import
from river import metrics
I/O Contract
| Method | Parameters | Returns | Description |
|---|---|---|---|
| update | y_true, y_pred, [w] | None | Updates confusion matrix with new prediction |
| __getitem__ | key | dict | Returns row of confusion matrix for given true label |
| support | label | float | Returns number of times label appears as true label |
| true_positives | label | float | Returns TP count for label |
| false_positives | label | float | Returns FP count for label |
| false_negatives | label | float | Returns FN count for label |
| true_negatives | label | float | Returns TN count for label |
Usage Examples
from river import metrics
y_true = ['cat', 'ant', 'cat', 'cat', 'ant', 'bird']
y_pred = ['ant', 'ant', 'cat', 'cat', 'ant', 'cat']
cm = metrics.ConfusionMatrix()
for yt, yp in zip(y_true, y_pred):
cm.update(yt, yp)
print(cm)
# ant bird cat
# ant 2 0 0
# bird 0 0 1
# cat 1 0 2
# Access specific cell
print(cm['bird']['cat'])
# 1.0
# Get class statistics
print(f"True positives for 'cat': {cm.true_positives('cat')}")
# True positives for 'cat': 2.0
print(f"False positives for 'cat': {cm.false_positives('cat')}")
# False positives for 'cat': 1.0
print(f"Support for 'cat': {cm.support('cat')}")
# Support for 'cat': 3.0
# Share confusion matrix between metrics to reduce computation
cm = metrics.ConfusionMatrix()
precision = metrics.Precision(cm=cm)
recall = metrics.Recall(cm=cm)
for yt, yp in [(True, True), (False, True), (True, False)]:
precision.update(yt, yp)
recall.update(yt, yp)
print(f"Precision: {precision.get():.2%}, Recall: {recall.get():.2%}")
# Precision: 50.00%, Recall: 50.00%