-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathf_score.py
73 lines (50 loc) · 1.65 KB
/
f_score.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
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
#!/usr/bin/env python3
from functools import partial
import keras.backend as K
def fbeta_score_micro(y_true, y_pred, beta):
y_true = K.round(K.clip(y_true, 0, 1))
y_pred = K.round(K.clip(y_pred, 0, 1))
c1 = K.sum(y_true * y_pred)
c2 = K.sum(y_pred)
c3 = K.sum(y_true)
precision = c1 / (c2 + K.epsilon())
recall = c1 / (c3 + K.epsilon())
fbeta_score = (
(1 + beta ** 2)
* (precision * recall)
/ (precision * beta ** 2 + recall + K.epsilon())
)
return fbeta_score
def fbeta_score_macro(y_true, y_pred, beta):
y_true = K.round(K.clip(y_true, 0, 1))
y_pred = K.round(K.clip(y_pred, 0, 1))
c1 = K.sum(y_true * y_pred, axis=0)
c2 = K.sum(y_pred, axis=0)
c3 = K.sum(y_true, axis=0)
precision = c1 / (c2 + K.epsilon())
recall = c1 / (c3 + K.epsilon())
fbeta_score = (
(1 + beta ** 2)
* (precision * recall)
/ (precision * beta ** 2 + recall + K.epsilon())
)
return K.mean(fbeta_score)
def fbeta_score(beta, weight="micro"):
"""
Return the f_beta score metric function used by keras.
>>> y_true = K.constant([[0, 1, 0], [1, 0, 0], [1, 0, 0]])
>>> y_pred = K.constant([[0, 1, 0], [1, 0, 0], [0, 1, 0]])
>>> metric = fbeta_score(1, "micro")
>>> K.eval(metric(y_true, y_pred))
0.6666666
>>> metric = fbeta_score(1, "macro")
>>> K.eval(metric(y_true, y_pred))
0.4444444
"""
func = dict(micro=fbeta_score_micro, macro=fbeta_score_macro)[weight]
func = partial(func, beta=beta)
func.__name__ = f"f{beta}_score"
return func
if __name__ == "__main__":
import doctest
doctest.testmod()