File size: 1,931 Bytes
9342485
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
test_cases = [
    {
        "predictions": [0, 1, 0, 1, 0, 1],
        "references":  [0, 1, 0, 1, 0, 1],
        "sample_weight": None,
        "adjusted": False,
        "result": {"balanced_accuracy": 0}
    },
    {
        "predictions": [0, 0, 1, 1, 1, 1],
        "references":  [0, 0, 0, 0, 1, 1],
        "sample_weight": None,
        "adjusted": False,
        "result": {"balanced_accuracy": 0}
    },
    {
        "predictions": [0, 1, 1, 0, 1, 2],
        "references":  [0, 1, 2, 0, 1, 2],
        "sample_weight": None,
        "adjusted": False,
        "result": {"balanced_accuracy": 0}
    },
    {
        "predictions": [0, 0, 1, 2, 1, 2],
        "references":  [0, 0, 0, 0, 1, 2],
        "sample_weight": None,
        "adjusted": False,
        "result": {"balanced_accuracy": 0}
    },
    {
        "predictions": [0, 1, 1, 0, 0, 1],
        "references":  [0, 1, 0, 1, 0, 1],
        "sample_weight": [0.5, 0.7, 0.8, 0.9, 1.0, 0.6],
        "adjusted": False,
        "result": {"balanced_accuracy": 0}
    },
    {
        "predictions": [0, 1, 1, 0, 0, 1],
        "references":  [0, 1, 0, 1, 0, 1],
        "sample_weight": None,
        "adjusted": True,
        "result": {"balanced_accuracy": 0}
    },
]

import pytest
from evaluate import load
from sklearn.metrics import balanced_accuracy_score

@pytest.mark.parametrize("test_case", test_cases)
def test_balanced_accuracy(test_case):
    metric = load("hyperml/balanced_accuracy")
    result = metric.compute(
        predictions=test_case["predictions"],
        references=test_case["references"],
        sample_weight=test_case["sample_weight"],
        adjusted=test_case["adjusted"]
    )
    assert result["balanced_accuracy"] == balanced_accuracy_score(y_pred=test_case["predictions"], y_true=test_case["references"], sample_weight=test_case["sample_weight"], adjusted=test_case["adjusted"])
    assert result == test_case["result"]