-
Notifications
You must be signed in to change notification settings - Fork 3
/
measurement.py
162 lines (86 loc) · 3.1 KB
/
measurement.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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
import numpy as np
from sklearn.metrics import average_precision_score
def compute_AP(Prediction,Label):
num_class = Prediction.shape[1]
ap=np.zeros(num_class)
for idx_cls in range(num_class):
prediction = np.squeeze(Prediction[:,idx_cls])
label = np.squeeze(Label[:,idx_cls])
mask = np.abs(label)==1
if np.sum(label>0)==0:
continue
binary_label=np.clip(label[mask],0,1)
ap[idx_cls]=average_precision_score(binary_label,prediction[mask])#AP(prediction,label,names)
return ap
def confusion_matrix(p_class,labels,n_label = 200):
p_class = np.squeeze(p_class)
labels = np.squeeze(labels)
M = np.zeros((n_label,n_label))
for idx_l in range(n_label):
p_class_l=p_class[labels == idx_l]
for idx_l_2 in range(n_label):
M[idx_l,idx_l_2] = np.sum(p_class_l==idx_l_2)
return M
def compute_number_misclassified(Prediction,Label):
binary_Prediction = Prediction.copy()
binary_Prediction[Prediction>=0]=1
binary_Prediction[Prediction<0]=-1
Res=np.abs(binary_Prediction-Label)/2
Res_p = np.zeros(Label.shape)
Res_n = np.zeros(Label.shape)
Res_p[(Res>0)&(Label==1)]=1
Res_n[(Res>0)&(Label==-1)]=1
return np.sum(Res_p,0),np.sum(Res_n,0)
def apk(actual, predicted, k=10):
"""
Computes the average precision at k.
This function computes the average prescision at k between two lists of
items.
Parameters
----------
actual : list
A list of elements that are to be predicted (order doesn't matter)
predicted : list
A list of predicted elements (order does matter)
k : int, optional
The maximum number of predicted elements
Returns
-------
score : double
The average precision at k over the input lists
"""
#print('precision at '+str(k))
if len(predicted)>k:
predicted = predicted[:k]
score = 0.0
num_hits = 0.0
for i,p in enumerate(predicted):
if p in actual and p not in predicted[:i]:
num_hits += 1.0
score += num_hits / (i+1.0)
#
# if not actual:
#
# return 0.0
return score / min(len(actual), k)
def mapk(actual, predicted, k=10):
"""
Computes the mean average precision at k.
This function computes the mean average prescision at k between two lists
of lists of items.
Parameters
----------
actual : list
A list of lists of elements that are to be predicted
(order doesn't matter in the lists)
predicted : list
A list of lists of predicted elements
(order matters in the lists)
k : int, optional
The maximum number of predicted elements
Returns
-------
score : double
The mean average precision at k over the input lists
"""
return np.mean([apk(a,p,k) for a,p in zip(actual, predicted)])