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 Precision

From Leeroopedia


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

Overview

Precision metrics measuring the proportion of positive predictions that are actually correct.

Description

This module provides precision metrics that compute TP / (TP + FP), the fraction of positive predictions that are correct. It includes Precision (binary), MacroPrecision (unweighted average across classes), MicroPrecision (global precision from aggregated TP and FP), and WeightedPrecision (weighted by class support). Precision answers "Of all positive predictions, how many were correct?"

Usage

Use Precision when false positives are particularly costly in your application. High precision means few false alarms. Choose MacroPrecision for equal per-class treatment, MicroPrecision for overall performance across all predictions, or WeightedPrecision when you want to account for class imbalance. Precision is particularly important in scenarios like spam detection or medical diagnosis where false positives have serious consequences.

Code Reference

Source Location

Signature

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

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

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

class WeightedPrecision(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 precision score (0.0 to 1.0)

Usage Examples

from river import metrics

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

metric = metrics.Precision()

for yt, yp in zip(y_true, y_pred):
    metric.update(yt, yp)
    print(metric)
# Precision: 100.00%
# Precision: 50.00%
# Precision: 50.00%
# Precision: 66.67%
# Precision: 75.00%

# High precision (75%) means most positive predictions were correct
# Only 1 false positive out of 4 positive predictions

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

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

# Multi-class Micro Precision (global)
metric_micro = metrics.MicroPrecision()
for yt, yp in zip([0, 1, 2, 2, 2], [0, 0, 2, 2, 1]):
    metric_micro.update(yt, yp)
    print(metric_micro)
# MicroPrecision: 100.00%
# MicroPrecision: 50.00%
# MicroPrecision: 66.67%
# MicroPrecision: 75.00%
# MicroPrecision: 60.00%

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

print(metric_weighted)
# WeightedPrecision: 70.00%

Related Pages

Page Connections

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