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 Jaccard

From Leeroopedia


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

Overview

Jaccard index (Intersection over Union) for binary and multi-class classification similarity measurement.

Description

This module provides Jaccard index metrics that measure similarity as the intersection divided by union of predicted and true sets. It includes Jaccard (binary), MacroJaccard (unweighted average across classes), MicroJaccard (global intersection over union), and WeightedJaccard (weighted by class support). The formula is J = TP / (TP + FP + FN), ranging from 0 (no overlap) to 1 (perfect overlap).

Usage

Use Jaccard metrics when you want to measure the overlap between predicted and true sets, particularly useful in classification tasks where you care about the proportion of correct predictions relative to all predictions and actual positives combined. Macro variant averages per-class scores, micro computes globally, and weighted accounts for class imbalance.

Code Reference

Source Location

Signature

class Jaccard(metrics.base.BinaryMetric):
    def __init__(self, cm=None, pos_val=True):
        pass

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

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

class WeightedJaccard(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 Jaccard index (0.0 to 1.0)

Usage Examples

from river import metrics

# Binary Jaccard
y_true = [False, True, True]
y_pred = [True, True, True]

metric = metrics.Jaccard()

for yt, yp in zip(y_true, y_pred):
    metric.update(yt, yp)
    print(metric)
# Jaccard: 0.00%
# Jaccard: 50.00%
# Jaccard: 66.67%

# Multi-class Macro Jaccard (unweighted average)
y_true = [0, 1, 2, 2, 2]
y_pred = [0, 0, 2, 2, 1]

metric_macro = metrics.MacroJaccard()

for yt, yp in zip(y_true, y_pred):
    metric_macro.update(yt, yp)
    print(metric_macro)
# MacroJaccard: 100.00%
# MacroJaccard: 25.00%
# MacroJaccard: 50.00%
# MacroJaccard: 50.00%
# MacroJaccard: 38.89%

# Multi-class Micro Jaccard (global)
metric_micro = metrics.MicroJaccard()
for yt, yp in zip([0, 1, 2, 2, 2], [0, 0, 2, 2, 1]):
    metric_micro.update(yt, yp)

print(metric_micro)
# MicroJaccard: 42.86%

# Multi-class Weighted Jaccard (by class support)
metric_weighted = metrics.WeightedJaccard()
for yt, yp in zip([0, 1, 2, 2, 2], [0, 0, 2, 2, 1]):
    metric_weighted.update(yt, yp)

print(metric_weighted)
# WeightedJaccard: 50.00%

Related Pages

Page Connections

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