-
Notifications
You must be signed in to change notification settings - Fork 1
/
metric.py
executable file
·76 lines (67 loc) · 2.85 KB
/
metric.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
74
75
import numpy as np
from sklearn.metrics import mean_squared_error
from scipy.stats import pearsonr, spearmanr
import pdb
def precision_k(label_test, label_predict, k, remove_zero = False):
# if remove_zero:
# label_filter = label_test != 0
# label_test = label_test[label_filter]
# label_predict = label_predict[label_filter]
precision_k_neg = []
precision_k_pos = []
for i in range(len(label_test)):
label_test_i = label_test[i]
label_predict_i = label_predict[i]
if remove_zero:
label_filter = label_test_i != 0
label_test_i = label_test_i[label_filter]
label_predict_i = label_predict_i[label_filter]
num_pos = 100
num_neg = 100
label_test_i = np.argsort(label_test_i)
label_predict_i = np.argsort(label_predict_i)
neg_test_set = set(label_test_i[:num_neg])
pos_test_set = set(label_test_i[-num_pos:])
neg_predict_set = set(label_predict_i[:k])
pos_predict_set = set(label_predict_i[-k:])
precision_k_neg.append(len(neg_test_set.intersection(neg_predict_set)) / k)
precision_k_pos.append(len(pos_test_set.intersection(pos_predict_set)) / k)
return np.mean(precision_k_neg), np.mean(precision_k_pos)
# def rmse(label_test, label_predict, remove_zero = False):
# if remove_zero:
# mask = label_test != 0
# return np.sqrt(mean_squared_error(label_test, label_predict * mask))
# else:
return np.sqrt(mean_squared_error(label_test, label_predict))
def rmse(label_test, label_predict, remove_zero = False):
if remove_zero:
mask = label_test != 0
return np.sqrt(mean_squared_error(label_test[mask], label_predict[mask]))
else:
return np.sqrt(mean_squared_error(label_test, label_predict))
def correlation(label_test, label_predict, correlation_type, remove_zero = False):
if correlation_type == 'pearson':
corr = pearsonr
elif correlation_type == 'spearman':
corr = spearmanr
else:
raise ValueError("Unknown correlation type: %s" % correlation_type)
score = []
if len(label_test.shape) == 1:
if remove_zero:
zero_filter = (label_test == 0)
label_test = label_test[~zero_filter]
label_predict = label_predict[~zero_filter]
score.append(corr(label_test, label_predict)[0])
else:
for lb_test, lb_predict in zip(label_test, label_predict):
if remove_zero:
zero_filter = (lb_test == 0)
lb_test = lb_test[~zero_filter]
lb_predict = lb_predict[~zero_filter]
if sum(lb_predict) == 0:
lb_predict[0] = 0.00001
score.append(corr(lb_test, lb_predict)[0])
if np.isnan(np.mean(score)):
pdb.set_trace()
return np.mean(score), score