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 MCC

From Leeroopedia


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

Overview

Matthews Correlation Coefficient measuring binary classification quality accounting for all confusion matrix categories.

Description

MCC (Matthews Correlation Coefficient) is a balanced measure for binary classification that takes into account true positives, true negatives, false positives, and false negatives. It returns a value between -1 (total disagreement) and +1 (perfect prediction), with 0 indicating random prediction. MCC is particularly useful for imbalanced datasets as it provides a balanced measure even when classes are of very different sizes.

Usage

Use MCC when evaluating binary classifiers on imbalanced datasets or when you need a single metric that considers all four confusion matrix categories equally. Unlike accuracy, precision, or recall which can be misleading on imbalanced data, MCC provides a balanced assessment. It's especially valuable when both false positives and false negatives are important, and class sizes differ significantly.

Code Reference

Source Location

Signature

class MCC(metrics.base.BinaryMetric):
    def __init__(self, cm=None, pos_val=True):
        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 MCC score (-1.0 to 1.0)

Usage Examples

from river import metrics

y_true = [True, True, True, False]
y_pred = [True, False, True, True]

mcc = metrics.MCC()

for yt, yp in zip(y_true, y_pred):
    mcc.update(yt, yp)

print(mcc)
# MCC: -0.333333

# Interpretation:
# MCC = 1:  Perfect prediction
# MCC = 0:  Random prediction
# MCC = -1: Total disagreement (inverse prediction)

# The negative value (-0.333) indicates the classifier
# performs worse than random guessing

# Compare with a better classifier
y_pred_better = [True, True, True, False]
mcc_better = metrics.MCC()

for yt, yp in zip(y_true, y_pred_better):
    mcc_better.update(yt, yp)

print(mcc_better)
# MCC: 1.0 (perfect prediction)

# MCC is robust to class imbalance
# Example with imbalanced data (90% negative class)
y_true_imb = [False] * 9 + [True]
y_pred_imb = [False] * 8 + [True, True]

mcc_imb = metrics.MCC()
for yt, yp in zip(y_true_imb, y_pred_imb):
    mcc_imb.update(yt, yp)

print(f"MCC: {mcc_imb.get():.3f}")
# MCC still provides meaningful evaluation despite imbalance

Related Pages

Page Connections

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