Skip to content

Classification: precision, recall and F1

← Classification

An alarm says "a crash is coming" or says nothing. Precision tells how often the alarm is right when it rings. Recall tells how many of the real crashes it caught. F1 puts the two in one number, so an alarm that rings every day, or never, cannot score well. A robot controller that flags faults is judged the same way, in the last section.

The running F1 of a crash alarm on a stock index for 25 alarm thresholds: it jumps for a few dozen days and then settles.

1. A first example: an alarm for the crash days of a stock index

Ten trading days: 1 is a crash day and 0 is a normal day. The alarm rings (1) or stays quiet (0).

from dense_armor.core.evaluation.metrics import F1, Precision, Recall

crash = [0, 0, 1, 0, 0, 1, 0, 0, 0, 1]
alarm = [0, 1, 1, 0, 0, 1, 0, 1, 0, 0]
ms = [Precision(positive=1), Recall(positive=1), F1(positive=1)]
for truth, flag in zip(crash, alarm):
    for m in ms:
        m.update(truth, flag)
print([round(m.get(), 3) for m in ms])
[0.5, 0.667, 0.571]

Count by hand. The alarm rang on days 2, 3, 6 and 8. On days 3 and 6 there was a crash: two true positives, TP = 2. On days 2 and 8 there was none: two false positives, FP = 2. The crash of day 10 was missed: one false negative, FN = 1. So P = TP/(TP+FP) = 2/4 = 0.5, R = TP/(TP+FN) = 2/3 = 0.667 and F1 = 2PR/(P+R) = 0.571.

A stock index over 250 trading days: the running Precision, Recall and F1 of a crash alarm at threshold 1.0.

On 250 trading days with 18 crash days the same kind of alarm rang 55 times. After 250 days the precision reads 0.236, the recall 0.722 and the F1 0.356: the alarm catches most crashes, but most of its rings are false.

The picture follows the same 400 trading days for 25 alarms that ring when a score passes a threshold between 0 and 3. Each point of the surface is the F1 after that many days. After 400 days the alarm with threshold 1.62 reads 0.553, the best of the 25; the alarm that rings every day (threshold 0.0) reads 0.315, and the alarm that almost never rings (threshold 3.0) reads 0.12.

class what it does
Precision of the days the alarm rang, the share that really were crashes
Recall of the crash days, the share the alarm caught
F1 the harmonic mean of precision and recall
FBeta the same mean with a weight beta on recall
Accuracy the share of right answers, the metric of the page before this one

2. What the metric keeps

Precision, Recall and F1 keep three counts for the positive class: true positives (tp_), false positives (fp_) and false negatives (fn_). The score is computed from them at any moment, and after a million days the metric still keeps three counts.

\[P = \frac{TP}{TP+FP} \qquad R = \frac{TP}{TP+FN} \qquad F_1 = \frac{2PR}{P+R}\]

In the example TP is 2, FP is 2 and FN is 1, so P = 2/4, R = 2/3 and F1 = 0.571.

Source: the docstring of the module dense_armor.core.evaluation.metrics.classification says that F1 is the harmonic mean of precision and recall and cites Sasaki, Y. (2007). The truth of the F-measure. Teach Tutor Mater. The docstrings of Precision and Recall cite no paper: precision is the fraction of predicted positives that are correct and recall the fraction of true positives that are detected. The docstring of F1 cites none either; it says F1 is a thin subclass of FBeta with beta = 1.0.

3. Giving recall more weight

A missed crash costs more than a false alarm, so the trader wants recall to count more. FBeta is the weighted harmonic mean, and F1 is the case beta = 1.

\[F_\beta = \frac{(1+\beta^2)\,P\,R}{\beta^2 P + R}\]
from dense_armor.core.evaluation.metrics import FBeta

crash = [0, 0, 1, 0, 0, 1, 0, 0, 0, 1]
alarm = [0, 1, 1, 0, 0, 1, 0, 1, 0, 0]
m = FBeta(beta=2.0, positive=1)
for truth, flag in zip(crash, alarm):
    m.update(truth, flag)
print(round(m.get(), 3))
0.625

With beta = 2 the formula gives 5 × 0.5 × 0.667 / (4 × 0.5 + 0.667) = 0.625, above the F1 of 0.571 because the recall (0.667) is higher than the precision (0.5).

Source: the docstring of FBeta gives the formula F_beta = (1 + beta**2) * P * R / (beta**2 * P + R) and cites no paper.

4. Combine two shards

merge joins two metrics that read different parts of the stream; the counts are added, so the result is the same as reading the whole stream.

from dense_armor.core.evaluation.metrics import Precision

crash = [0, 0, 1, 0, 0, 1, 0, 0, 0, 1]
alarm = [0, 1, 1, 0, 0, 1, 0, 1, 0, 0]
a, b = Precision(positive=1), Precision(positive=1)
for truth, flag in zip(crash[:5], alarm[:5]):
    a.update(truth, flag)
for truth, flag in zip(crash[5:], alarm[5:]):
    b.update(truth, flag)
print(a.merge(b).get())
0.5

5. The parameters

Parameter Meaning Default
positive class treated as "positive" for precision/recall True
average how to combine per-class metrics: "binary", "macro", "micro", "weighted" "binary"
beta weight for FBeta: higher favors recall 1.0
eps small constant to avoid log(0) in LogLoss (see the Probabilities page) 1e-15

average="macro" takes the mean of the per-class scores, average="micro" pools the counts, and average="weighted" weights each class by how often it occurs.

6. The same scores on a robot joint

A joint controller flags a fault with 1 and no fault with 0, and the maintenance log says what was true. The same metrics read the pairs, here with the macro average of F1 next to the binary one.

from dense_armor.core.evaluation.metrics import F1, Precision, Recall

y = [1, 0, 1, 1, 0, 1]
p = [1, 1, 1, 0, 0, 1]
ms = [Precision(positive=1), Recall(positive=1), F1(positive=1), F1(average="macro")]
for a, b in zip(y, p):
    for m in ms:
        m.update(a, b)
print([round(m.get(), 3) for m in ms])
[0.75, 0.75, 0.75, 0.625]

Count by hand for class 1: true positives TP = 3 (positions 0, 2, 5), one false positive FP (position 1), one false negative FN (position 3).

P = TP/(TP+FP) = 3/4, R = TP/(TP+FN) = 3/4, F_1 = 2PR/(P+R) = 0.75.

For class 0: TP = 1, FP = 1, FN = 1, so F1 = 0.5. The macro F1 is (0.75 + 0.5) / 2 = 0.625. average="micro" pools the counts; average="weighted" weights by class frequency; FBeta(beta=2) weights recall more.

API reference

Accuracy

Accuracy()

Bases: Metric

Fraction of samples where y_pred == y_true.

Examples:

>>> from dense_armor.core.evaluation.metrics import Accuracy
>>> m = Accuracy()
>>> for a, b in [(0, 0), (1, 1), (0, 1), (1, 1)]:
...     _ = m.update(a, b)
>>> m.get()
0.75
Source code in dense_armor/core/evaluation/metrics/classification.py
def __init__(self) -> None:
    super().__init__()
    self.hits_ = 0.0
    self.total_ = 0.0

merge

merge(other: Metric) -> Accuracy

Combine two partial states.

Parameters:

Name Type Description Default
other Metric

another :class:Accuracy on a disjoint set of pairs.

required

Returns:

Type Description
Accuracy

A new :class:Accuracy with the combined state.

Raises:

Type Description
TypeError

if other is not an :class:Accuracy.

Source code in dense_armor/core/evaluation/metrics/classification.py
def merge(self, other: Metric) -> "Accuracy":
    """Combine two partial states.

    Args:
        other: another :class:`Accuracy` on a disjoint set of pairs.

    Returns:
        A new :class:`Accuracy` with the combined state.

    Raises:
        TypeError: if ``other`` is not an :class:`Accuracy`.
    """
    if not isinstance(other, Accuracy):
        raise TypeError(
            f"merge expects Accuracy, got {type(other).__name__}"
        )
    m = Accuracy()
    m.hits_ = self.hits_ + other.hits_
    m.total_ = self.total_ + other.total_
    m.n_ = self.n_ + other.n_
    m.n_missing_ = self.n_missing_ + other.n_missing_
    return m

Precision

Precision(positive: Any = True, average: str = 'binary')

Bases: _PRFBase

Fraction of predicted positives that are correct.

Supports four averages: "binary" (uses positive), "macro", "micro", "weighted".

Examples:

>>> from dense_armor.core.evaluation.metrics import Precision
>>> m = Precision(positive=1, average="binary")
>>> for a, b in [(1, 1), (0, 1), (1, 1), (0, 0)]:
...     _ = m.update(a, b)
>>> m.get()
0.6666666666666666
Source code in dense_armor/core/evaluation/metrics/classification.py
def __init__(
    self, positive: Any = True, average: str = "binary"
) -> None:
    super().__init__()
    self.positive = positive
    self.average = average
    self.tp_: dict[Any, float] = {}
    self.fp_: dict[Any, float] = {}
    self.fn_: dict[Any, float] = {}
    self.support_: dict[Any, float] = {}

merge

merge(other: Metric) -> Precision

Combine two partial states.

Parameters:

Name Type Description Default
other Metric

another :class:Precision with the same configuration.

required

Returns:

Type Description
Precision

A new :class:Precision with the combined state.

Raises:

Type Description
TypeError

if other is not a :class:Precision.

Source code in dense_armor/core/evaluation/metrics/classification.py
def merge(self, other: Metric) -> "Precision":
    """Combine two partial states.

    Args:
        other: another :class:`Precision` with the same configuration.

    Returns:
        A new :class:`Precision` with the combined state.

    Raises:
        TypeError: if ``other`` is not a :class:`Precision`.
    """
    if not isinstance(other, Precision):
        raise TypeError(
            f"merge expects Precision, got {type(other).__name__}"
        )
    m = Precision(positive=self.positive, average=self.average)
    self._merge_into(m, other)
    m.n_ = self.n_ + other.n_
    m.n_missing_ = self.n_missing_ + other.n_missing_
    return m

Recall

Recall(positive: Any = True, average: str = 'binary')

Bases: _PRFBase

Fraction of true positives that are detected.

Same four averages as :class:Precision.

Examples:

>>> from dense_armor.core.evaluation.metrics import Recall
>>> m = Recall(positive=1, average="binary")
>>> for a, b in [(1, 1), (0, 1), (1, 0)]:
...     _ = m.update(a, b)
>>> m.get()
0.5
Source code in dense_armor/core/evaluation/metrics/classification.py
def __init__(
    self, positive: Any = True, average: str = "binary"
) -> None:
    super().__init__()
    self.positive = positive
    self.average = average
    self.tp_: dict[Any, float] = {}
    self.fp_: dict[Any, float] = {}
    self.fn_: dict[Any, float] = {}
    self.support_: dict[Any, float] = {}

merge

merge(other: Metric) -> Recall

Combine two partial states.

Parameters:

Name Type Description Default
other Metric

another :class:Recall with the same configuration.

required

Returns:

Type Description
Recall

A new :class:Recall with the combined state.

Raises:

Type Description
TypeError

if other is not a :class:Recall.

Source code in dense_armor/core/evaluation/metrics/classification.py
def merge(self, other: Metric) -> "Recall":
    """Combine two partial states.

    Args:
        other: another :class:`Recall` with the same configuration.

    Returns:
        A new :class:`Recall` with the combined state.

    Raises:
        TypeError: if ``other`` is not a :class:`Recall`.
    """
    if not isinstance(other, Recall):
        raise TypeError(f"merge expects Recall, got {type(other).__name__}")
    m = Recall(positive=self.positive, average=self.average)
    self._merge_into(m, other)
    m.n_ = self.n_ + other.n_
    m.n_missing_ = self.n_missing_ + other.n_missing_
    return m

F1

F1(positive: Any = True, average: str = 'binary')

Bases: FBeta

F1 score: the harmonic mean of precision and recall.

A thin subclass of :class:FBeta with beta = 1.0.

Examples:

>>> from dense_armor.core.evaluation.metrics import F1
>>> m = F1(positive=1)
>>> for a, b in [(1, 1), (0, 1), (1, 1)]:
...     _ = m.update(a, b)
>>> round(m.get(), 4)
0.8
Source code in dense_armor/core/evaluation/metrics/classification.py
def __init__(self, positive: Any = True, average: str = "binary") -> None:
    super().__init__(beta=1.0, positive=positive, average=average)

merge

merge(other: Metric) -> F1

Combine two partial states, keeping the F1 class.

Parameters:

Name Type Description Default
other Metric

another :class:F1.

required

Returns:

Type Description
F1

A new :class:F1 with the combined state.

Raises:

Type Description
TypeError

if other is not an :class:F1.

Source code in dense_armor/core/evaluation/metrics/classification.py
def merge(self, other: Metric) -> "F1":
    """Combine two partial states, keeping the F1 class.

    Args:
        other: another :class:`F1`.

    Returns:
        A new :class:`F1` with the combined state.

    Raises:
        TypeError: if ``other`` is not an :class:`F1`.
    """
    if not isinstance(other, F1):
        raise TypeError(
            f"merge expects F1, got {type(other).__name__}"
        )
    m = F1(positive=self.positive, average=self.average)
    self._merge_into(m, other)
    m.n_ = self.n_ + other.n_
    m.n_missing_ = self.n_missing_ + other.n_missing_
    return m

FBeta

FBeta(
    beta: float = 1.0,
    positive: Any = True,
    average: str = "binary",
)

Bases: _PRFBase

Weighted harmonic mean of precision and recall.

F_beta = (1 + beta**2) * P * R / (beta**2 * P + R). F1 is the special case beta = 1. The average follows the same conventions as :class:Precision.

Parameters:

Name Type Description Default
beta float

relative weight of recall over precision.

1.0
positive Any

the label treated as positive in "binary" mode.

True
average str

"binary", "macro", "micro" or "weighted".

'binary'

Examples:

>>> from dense_armor.core.evaluation.metrics import FBeta
>>> m = FBeta(beta=1.0, positive=1)
>>> for a, b in [(1, 1), (0, 1), (1, 1), (0, 0)]:
...     _ = m.update(a, b)
>>> round(m.get(), 4)
0.8
Source code in dense_armor/core/evaluation/metrics/classification.py
def __init__(
    self,
    beta: float = 1.0,
    positive: Any = True,
    average: str = "binary",
) -> None:
    super().__init__(positive=positive, average=average)
    self.beta = beta

merge

merge(other: Metric) -> FBeta

Combine two partial states.

Parameters:

Name Type Description Default
other Metric

another :class:FBeta with the same beta and average.

required

Returns:

Type Description
FBeta

A new :class:FBeta with the combined state.

Raises:

Type Description
TypeError

if other is not an :class:FBeta.

Source code in dense_armor/core/evaluation/metrics/classification.py
def merge(self, other: Metric) -> "FBeta":
    """Combine two partial states.

    Args:
        other: another :class:`FBeta` with the same ``beta`` and
            ``average``.

    Returns:
        A new :class:`FBeta` with the combined state.

    Raises:
        TypeError: if ``other`` is not an :class:`FBeta`.
    """
    if not isinstance(other, FBeta):
        raise TypeError(f"merge expects FBeta, got {type(other).__name__}")
    if self.beta != other.beta or self.average != other.average:
        raise ValueError("merge requires the same beta and average")
    m = FBeta(
        beta=self.beta, positive=self.positive, average=self.average
    )
    self._merge_into(m, other)
    m.n_ = self.n_ + other.n_
    m.n_missing_ = self.n_missing_ + other.n_missing_
    return m

Details

Choices

  • Base implementation: Accuracy inherits from Metric and implements _update by incrementing hits when \(y = \hat{y}\).
  • Merge: two Accuracy metrics with \(n_1, hits_1\) and \(n_2, hits_2\) merge to \(n_1+n_2, hits_1+hits_2\); two Precision or Recall metrics add their counts.
  • Revert: when revert(y, p) is called, counts are decremented appropriately for use in Rolling windows.
  • The figures are drawn with the library by docs/assets/metrics/make_classification_figures.py. The surface uses one stream of 400 days with a crash on about 10% of the days and 25 thresholds from 0 to 3; the second figure uses 250 days and the threshold 1.0.
  • The next pages of this section: confusion matrix and balanced accuracy and Cohen's kappa.