File size: 2,603 Bytes
d0c77e3
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
from unittest import TestCase

from precision_recall_fscore_accuracy import PrecisionRecallFscoreAccuracy


class PrecisionRecallFscoreAccuracyTestBinary(TestCase):
    """
    All of these tests are also used for multiset configuration. So please mind this and write the test in a way that
    it is valid for both configurations (do not use same label multiple times).
    """

    def setUp(self):
        self.metric = PrecisionRecallFscoreAccuracy(average="binary")

    def test_eok(self):
        self.assertDictEqual(
            {
                "precision": 1.0,
                "recall": 1.0,
                "fscore": 1.0,
                "accuracy": 1.0,
            },
            self.metric.compute(
                predictions=[0, 1, 0],
                references=[0, 1, 0]
            )
        )

    def test_eok_string(self):
        self.assertDictEqual(
            {
                "precision": 1.0,
                "recall": 1.0,
                "accuracy": 1.0,
                "fscore": 1.0
            },
            self.metric.compute(
                predictions=["0", "1", "0"],
                references=["0", "1", "0"]
            )
        )

    def test_completely_different(self):
        self.assertDictEqual(
            {
                "precision": 0.0,
                "recall": 0.0,
                "accuracy": 0.0,
                "fscore": 0.0
            },
            self.metric.compute(
                predictions=[0, 1, 0],
                references=[1, 0, 1]
            )
        )

    def test_max_precision(self):
        self.assertDictEqual(
            {
                "precision": 1.0,
                "recall": 0.5,
                "accuracy": 0.5,
                "fscore": 2 / 3
            },
            self.metric.compute(
                predictions=[0, 1],
                references=[1, 1]
            )
        )

    def test_max_recall(self):
        self.assertDictEqual(
            {
                "precision": 0.5,
                "recall": 1.0,
                "accuracy": 0.5,
                "fscore": 2 / 3
            },
            self.metric.compute(
                predictions=[1, 1],
                references=[1, 0]
            )
        )

    def test_partial_match(self):
        self.assertDictEqual(
            {
                "precision": 0.5,
                "recall": 0.5,
                "accuracy": 0.5,
                "fscore": 0.5
            },
            self.metric.compute(
                predictions=[0, 1, 0, 1],
                references=[1, 1, 0, 0]
            )
        )