-
Notifications
You must be signed in to change notification settings - Fork 0
/
metrics.py
56 lines (45 loc) · 1.97 KB
/
metrics.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
from typing import Sequence, Union
import numpy as np
import scipy.stats
from registry import registry
@registry.register_metric('mse')
def mean_squared_error(target: Sequence[float],
prediction: Sequence[float]) -> float:
target_array = np.asarray(target)
prediction_array = np.asarray(prediction)
return np.mean(np.square(target_array - prediction_array))
@registry.register_metric('mae')
def mean_absolute_error(target: Sequence[float],
prediction: Sequence[float]) -> float:
target_array = np.asarray(target)
prediction_array = np.asarray(prediction)
return np.mean(np.abs(target_array - prediction_array))
@registry.register_metric('spearmanr')
def spearmanr(target: Sequence[float],
prediction: Sequence[float]) -> float:
target_array = np.asarray(target)
prediction_array = np.asarray(prediction)
return scipy.stats.spearmanr(target_array, prediction_array).correlation
@registry.register_metric('pearsonr')
def spearmanr(target: Sequence[float],
prediction: Sequence[float]) -> float:
target_array = np.asarray(target)
prediction_array = np.asarray(prediction)
return scipy.stats.pearsonr(target_array, prediction_array).correlation
@registry.register_metric('accuracy')
def accuracy(target: Union[Sequence[int], Sequence[Sequence[int]]],
prediction: Union[Sequence[float], Sequence[Sequence[float]]]) -> float:
if isinstance(target[0], int):
# non-sequence case
return np.mean(np.asarray(target) == np.asarray(prediction).argmax(-1))
else:
correct = 0
total = 0
for label, score in zip(target, prediction):
label_array = np.asarray(label)
pred_array = np.asarray(score).argmax(-1)
mask = label_array != -1
is_correct = label_array[mask] == pred_array[mask]
correct += is_correct.sum()
total += is_correct.size
return correct / total