Skip to content

Precision and recall (precision_recall)#

Calculate the precision/recall of an estimated segmentation compared with the true segmentation.

Parameters:

Name Type Description Default
true_bkps list

list of the last index of each regime (true partition).

required
my_bkps list

list of the last index of each regime (computed partition).

required
margin int

allowed error (in points).

10

Returns:

Name Type Description
tuple

(precision, recall)

Source code in ruptures/metrics/precisionrecall.py
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
def precision_recall(true_bkps, my_bkps, margin=10):
    """Calculate the precision/recall of an estimated segmentation compared
    with the true segmentation.

    Args:
        true_bkps (list): list of the last index of each regime (true
            partition).
        my_bkps (list): list of the last index of each regime (computed
            partition).
        margin (int, optional): allowed error (in points).

    Returns:
        tuple: (precision, recall)
    """
    sanity_check(true_bkps, my_bkps)
    assert margin > 0, "Margin of error must be positive (margin = {})".format(margin)

    if len(my_bkps) == 1:
        return 0, 0

    used = set()
    true_pos = set(
        true_b
        for true_b, my_b in product(true_bkps[:-1], my_bkps[:-1])
        if my_b - margin < true_b < my_b + margin
        and not (my_b in used or used.add(my_b))
    )

    tp_ = len(true_pos)
    precision = tp_ / (len(my_bkps) - 1)
    recall = tp_ / (len(true_bkps) - 1)
    return precision, recall