-
Notifications
You must be signed in to change notification settings - Fork 83
/
precision.m
61 lines (48 loc) · 1.37 KB
/
precision.m
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
function [map precision_at_k] = precision(trn_label, trn_binary, tst_label, tst_binary, top_k, mode)
K = top_k;
QueryTimes = size(tst_binary,2);
correct = zeros(K,1);
total = zeros(K,1);
error = zeros(K,1);
AP = zeros(QueryTimes,1);
Ns = 1:1:K;
sum_tp = zeros(1, length(Ns));
for i = 1:QueryTimes
query_label = tst_label(i);
fprintf('query %d\n',i);
query_binary = tst_binary(:,i);
if mode==1
tic
similarity = pdist2(trn_binary',query_binary','hamming');
toc
fprintf('Complete Query [Hamming] %.2f seconds\n',toc);
elseif mode ==2
tic
similarity = pdist2(trn_binary',query_binary','euclidean');
toc
fprintf('Complete Query [Euclidean] %.2f seconds\n',toc);
end
[x2,y2]=sort(similarity);
buffer_yes = zeros(K,1);
buffer_total = zeros(K,1);
total_relevant = 0;
for j = 1:K
retrieval_label = trn_label(y2(j));
if (query_label==retrieval_label)
buffer_yes(j,1) = 1;
total_relevant = total_relevant + 1;
end
buffer_total(j,1) = 1;
end
% compute precision
P = cumsum(buffer_yes) ./ Ns';
if (sum(buffer_yes) == 0)
AP(i) = 0;
else
AP(i) = sum(P.*buffer_yes) / sum(buffer_yes);
end
sum_tp = sum_tp + cumsum(buffer_yes)';
end
precision_at_k = sum_tp ./ (Ns * QueryTimes);
map = mean(AP);
end