-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathMAP.py
61 lines (47 loc) · 1.32 KB
/
MAP.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
import torch
import pdb
import os
import numpy as np
import sklearn.metrics.pairwise as skp
import gc
import time
def comp_Ap(list_retrieval):
m=0;
Ap=0.;
for i in range(len(list_retrieval)):
if list_retrieval[i]:
m+=1
Ap+=m/(i+1)
return Ap/m
def comp_rc(binary,top):
r = 0;
for i in range(1,top+1):
if binary[i]:
r+=1
break
return r
def comp_MAp(ranks,clusters,similarity):
MAp=0;
recall = [0]*3;
top = [1,5,10]
#f = open('101_circle_long.txt','w')
for i in range(ranks.shape[0]):
binary=[clusters[i]==clusters[j] for j in ranks[i]]
MAp+=comp_Ap(binary)
for j in range(3):
r = comp_rc(binary,top[j])
#if j == 0:
# f.write(str(i)+';'+str(ranks[i][:11])+ str(list(similarity[i][ranks[i][:11]]))+'\n')
recall[j] += r
#f.close()
recall=[r/ranks.shape[0] for r in recall]
return MAp/ranks.shape[0],recall
def Test(dataset,clusters):
st = time.time()
similarity = torch.mm(dataset, dataset.t()).cpu().numpy()
print('distance_time:',time.time() - st)
st = time.time()
ranks=np.argsort(-similarity)
print('rank_time:',time.time() - st)
MAp,recall = comp_MAp(ranks,clusters,similarity);
return MAp,recall