forked from MCV-2022-M1-Project/Team5
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscores.py
156 lines (128 loc) · 4.09 KB
/
scores.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
from main import *
def apk(actual, predicted, k):
"""
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
"""
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
score = score / min(len(actual), k)
return score / min(len(actual), k)
def mapk(actual, predicted, k, ):
"""
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)])
def apk2(actual, predicted, k):
"""
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
"""
scores=[]
for i in range(len(predicted)):
predicted_part = predicted[i]
actual_part = [actual[i]]
if len(predicted_part)>k:
predicted_part = predicted_part[:k]
score = 0.0
num_hits = 0.0
for i,p in enumerate(predicted_part):
if p in actual_part and p not in predicted_part[:i]:
num_hits += 1.0
score += num_hits / (i+1.0)
if not actual_part:
return 0.0
score = score / min(len(actual_part), k)
scores.append(score / min(len(actual_part), k))
if len(predicted) == 2:
score = np.mean(scores)
else:
score = scores[0]
return score
def mapk2(actual, predicted, k, ):
"""
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
"""
# predicted2=[]
# for i in range(len(predicted)):
# predicted2.append([])
# for j in range(len(predicted[i])):
# predicted2[i].append([predicted[i][j]])
count = 0
result = 0
for i in range(len(predicted)):
image = predicted[i]
for j in range(len(image)):
count+=1
painting = predicted[i][j]
solution = [actual[i][j]]
apk_res = apk(solution,painting,k)
result +=apk_res
average = result / count
return average