-
Notifications
You must be signed in to change notification settings - Fork 93
/
linear_weighted_kappa.py
36 lines (31 loc) · 1.04 KB
/
linear_weighted_kappa.py
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
"""Cohen’s Kappa with linear weights"""
import typing
import numpy as np
from h2oaicore.metrics import CustomScorer
from sklearn.metrics import cohen_kappa_score
from sklearn.preprocessing import LabelEncoder
class QuadraticWeightedKappaScorer(CustomScorer):
_description = "Cohen’s kappa with linear weights: a statistic that measures inter-annotator agreement."
_multiclass = True
_maximize = True
_perfect_score = 1
_display_name = "COHEN_KAPPA"
def score(
self,
actual: np.array,
predicted: np.array,
sample_weight: typing.Optional[np.array] = None,
labels: typing.Optional[np.array] = None,
**kwargs
) -> float:
lb = LabelEncoder()
labels = lb.fit_transform(labels)
actual = lb.transform(actual)
predicted = np.argmax(predicted, axis=1)
return cohen_kappa_score(
actual,
predicted,
labels=labels,
weights="linear",
sample_weight=sample_weight,
)