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.
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:
- Problem type:
binary,multiclassormultilabel. - Statistic: The formula to compute, e.g.
with_accuracyorwith_f1. - Aggregation: How to combine the per-class values:
micro,macro,weightedorper_class.binary()andmulticlass()withtop_kselectmicrothemselves.
Usage
Binary Classification (Accuracy)
For a binary problem, you set a probability threshold, usually 0.5.
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.
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.
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.
API Reference
d9d.metric.impl.classification.BinaryAUROCMetric
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 |
required |
labels
|
Tensor
|
Ground truth binary labels, with as many elements as |
required |
Raises:
| Type | Description |
|---|---|
ValueError
|
If |
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)
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 |
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()
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()
with_fbeta(beta)
with_precision()
with_recall()
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. |