Skip to content

Classification Metrics

About

d9d provides distributed classification metrics: an approximate binary AUROC and metrics computed from a confusion matrix, such as accuracy, precision, recall and F-beta. Their state has a fixed size, so it does not grow with the number of samples.

Binary AUROC

BinaryAUROCMetric approximates AUROC with histograms of predicted probabilities, as described by Albakour et al., 2021. More bins give a more accurate result and use more memory.

1
2
3
from d9d.metric.impl.classification import BinaryAUROCMetric

auroc = BinaryAUROCMetric()

Confusion Matrix-Based Metrics

Accuracy, precision, recall and F-beta are all computed from a confusion matrix. confusion_matrix_metric() returns a builder that defines such a metric in three steps:

  1. Problem type: binary, multiclass or multilabel.
  2. Statistic: The formula to compute, e.g. with_accuracy or with_f1.
  3. Aggregation: How to combine the per-class values: micro, macro, weighted or per_class. binary() and multiclass() with top_k select micro themselves.

Usage

Binary Classification (Accuracy)

For a binary problem, you set a probability threshold, usually 0.5.

1
2
3
4
5
6
7
8
from d9d.metric.impl.classification import confusion_matrix_metric

accuracy = (
    confusion_matrix_metric()
    .binary(threshold=0.5)
    .with_accuracy()
    .build()
)

Multiclass Classification (Top-5 Accuracy)

Pass top_k to check whether the correct label is among the \(K\) highest predicted scores. Each prediction is then a single hit or miss, so the problem becomes a binary one.

1
2
3
4
5
6
7
8
from d9d.metric.impl.classification import confusion_matrix_metric

top5_acc = (
    confusion_matrix_metric()
    .multiclass(num_classes=1000, top_k=5)
    .with_accuracy()
    .build()
)

Multiclass Classification (Per-Class Precision)

To inspect individual classes, use the .per_class() aggregation. It returns a separate value, such as precision, for every class.

1
2
3
4
5
6
7
8
9
from d9d.metric.impl.classification import confusion_matrix_metric

per_class_precision = (
    confusion_matrix_metric()
    .multiclass(num_classes=10)
    .with_precision()
    .per_class()
    .build()
)

Multilabel Classification (Macro F1 Score)

In a multilabel problem, several classes can be correct at the same time. Each class is compared against the probability threshold independently. The .macro() aggregation averages the statistic, e.g. the F1 score, over all classes with equal weight.

1
2
3
4
5
6
7
8
9
from d9d.metric.impl.classification import confusion_matrix_metric

macro_f1 = (
    confusion_matrix_metric()
    .multilabel(num_classes=8, threshold=0.5)
    .with_f1()
    .macro()
    .build()
)

API Reference

d9d.metric.impl.classification.BinaryAUROCMetric

Bases: Metric[Tensor]

Approximate AUROC metric for binary classification, computed from histograms.

Exact AUROC needs all predictions to sort and rank them. This metric keeps two histograms of predicted probabilities instead, one for positive and one for negative samples. It estimates the area with the trapezoidal rule, so memory does not grow with the number of samples. If one class is absent, the result is 0.5.

References

Albakour et al., Fast and memory efficient AUC-ROC approximation for Stream Learning, 2021.

__init__(num_bins=10000)

Constructs the BinaryAUROCMetric object.

Parameters:

Name Type Description Default
num_bins int

Number of histogram bins. More bins use more memory and give a more accurate result.

10000

update(probs, labels)

Adds a batch of predictions to the histograms.

Parameters:

Name Type Description Default
probs Tensor

Predicted probabilities in [0, 1]. Any shape.

required
labels Tensor

Ground truth binary labels, with as many elements as probs.

required

Raises:

Type Description
ValueError

If probs and labels have a different number of elements.

d9d.metric.impl.classification.confusion_matrix_metric()

Creates a builder for a ConfusionMatrixMetric.

Returns:

Type Description
ConfusionMatrixMetricBuilder

A new builder.

d9d.metric.impl.classification.ConfusionMatrixMetricBuilder

Step-by-step builder of a ConfusionMatrixMetric.

Choose one problem type, one statistic and, unless the problem type sets it, one aggregation method. Then call build().

__init__()

Constructs the ConfusionMatrixMetricBuilder object.

binary(threshold=0.5)

Configures the metric for binary classification.

It also selects micro aggregation.

Parameters:

Name Type Description Default
threshold float

Predictions strictly above this value are positive.

0.5

Returns:

Type Description
Self

The current builder instance.

build()

Builds the configured ConfusionMatrixMetric.

Returns:

Type Description
ConfusionMatrixMetric

The configured metric.

Raises:

Type Description
ValueError

If the problem type, the statistic or the aggregation method is not configured.

macro()

Selects macro aggregation: the unweighted mean of the per-class metrics.

Returns:

Type Description
Self

The current builder instance.

micro()

Selects micro aggregation: the metric is computed from the confusion matrix summed over classes.

Returns:

Type Description
Self

The current builder instance.

multiclass(num_classes, top_k=None)

Configures the metric for multiclass classification.

Parameters:

Name Type Description Default
num_classes int

The number of mutually exclusive classes.

required
top_k int | None

If set, a prediction counts as correct when the target is among the top_k highest scores. This also selects micro aggregation.

None

Returns:

Type Description
Self

The current builder instance.

multilabel(num_classes, threshold=0.5)

Configures the metric for multilabel classification.

Parameters:

Name Type Description Default
num_classes int

The number of independent classes.

required
threshold float

Predictions strictly above this value are positive, for each class independently.

0.5

Returns:

Type Description
Self

The current builder instance.

per_class()

Selects no aggregation: the metric is returned for each class.

Returns:

Type Description
Self

The current builder instance.

weighted()

Selects weighted aggregation: the mean of the per-class metrics, weighted by class support.

The support of a class is its number of true instances.

Returns:

Type Description
Self

The current builder instance.

with_accuracy()

Selects accuracy as the statistic.

Returns:

Type Description
Self

The current builder instance.

with_aggregation(method)

Selects how the statistic is aggregated across classes.

Parameters:

Name Type Description Default
method ClassificationAggregationMethod

The aggregation method.

required

Returns:

Type Description
Self

The current builder instance.

with_f1()

Selects the F1 score as the statistic.

Returns:

Type Description
Self

The current builder instance.

with_fbeta(beta)

Selects the F-beta score as the statistic.

Parameters:

Name Type Description Default
beta float

The weight of recall relative to precision.

required

Returns:

Type Description
Self

The current builder instance.

with_precision()

Selects precision as the statistic.

Returns:

Type Description
Self

The current builder instance.

with_recall()

Selects recall as the statistic.

Returns:

Type Description
Self

The current builder instance.

with_statistic(statistic)

Selects a custom statistic.

Parameters:

Name Type Description Default
statistic ConfusionMatrixStatistic

The statistic to compute from the confusion matrix.

required

Returns:

Type Description
Self

The current builder instance.