Jump to content

Connect SuperML | Leeroopedia MCP: Equip your AI agents with best practices, code verification, and debugging knowledge. Powered by Leeroo — building Organizational Superintelligence. Contact us at founders@leeroo.com.

Implementation:Online ml River Metrics CrossEntropy

From Leeroopedia


Knowledge Sources
Domains Online_Learning, Evaluation_Metrics
Last Updated 2026-02-08 16:00 GMT

Overview

Multi-class cross-entropy loss metric, a generalization of logarithmic loss for multiple classes.

Description

CrossEntropy computes the cross-entropy loss between predicted probability distributions and true labels. It measures how well predicted probabilities match the true distribution, with lower values indicating better predictions. The metric requires probability distributions as predictions (dictionaries mapping class labels to probabilities) rather than hard class labels.

Usage

Use CrossEntropy to evaluate probabilistic classifiers that output class probability distributions. Unlike metrics that require hard labels, CrossEntropy evaluates the quality of probability estimates, penalizing confident wrong predictions more heavily. It's particularly useful for multi-class classification where you want to assess prediction confidence calibration.

Code Reference

Source Location

Signature

class CrossEntropy(metrics.base.MeanMetric, metrics.base.MultiClassMetric):
    def __init__(self, cm=None):
        pass

Import

from river import metrics

I/O Contract

Method Parameters Returns Description
update y_true (label), y_pred (dict) None Updates with true label and probability distribution
get - float Returns average cross-entropy loss (lower is better)

Note: requires_labels property returns False (requires probability distributions).

Usage Examples

from river import metrics

y_true = [0, 1, 2, 2]
y_pred = [
    {0: 0.29450637, 1: 0.34216758, 2: 0.36332605},
    {0: 0.21290077, 1: 0.32728332, 2: 0.45981591},
    {0: 0.42860913, 1: 0.33380113, 2: 0.23758974},
    {0: 0.44941979, 1: 0.32962558, 2: 0.22095463}
]

metric = metrics.CrossEntropy()

for yt, yp in zip(y_true, y_pred):
    metric.update(yt, yp)
    print(metric.get())
# 1.222454
# 1.169691
# 1.258864
# 1.321597

print(metric)
# CrossEntropy: 1.321598

# Lower cross-entropy indicates better probability estimates
# Perfect predictions would have cross-entropy approaching 0

Related Pages

Page Connections

Double-click a node to navigate. Hold to expand connections.
Principle
Implementation
Heuristic
Environment