-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnmi.m
66 lines (60 loc) · 1.33 KB
/
nmi.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
62
63
64
65
66
function [NMI, is_perfect] = nmi(subgroup, subgroup_est)
S = size(subgroup,2);
S_est = size(subgroup_est,2);
for s=1:S
if size(subgroup{s},1)==0
subgroup(s) = [];
end
end
for s=1:S_est
if size(subgroup_est{s},1)==0
subgroup_est(s) = [];
end
end
S = size(subgroup,2);
S_est = size(subgroup_est,2);
M_s = zeros(1,S);
M_s_est = zeros(1,S_est);
for s=1:S
M_s(s) = size(subgroup{s}, 2);
subgroup{s} = sort(subgroup{s});
end
for s=1:S_est
M_s_est(s) = size(subgroup_est{s}, 2);
subgroup_est{s} = sort(subgroup_est{s});
end
M = sum(M_s);
I = 0;
for s=1:S
for s_=1:S_est
K = size(intersect(subgroup{s},subgroup_est{s_}),2);
if K~=0
I = I + K/M*log(M*K/M_s(s)/M_s_est(s_));
end
end
end
H = -sum(M_s/M.*log(M_s/M));
H_est = -sum(M_s_est/M.*log(M_s_est/M));
NMI = 2*I/(H+H_est);
%% Is perfect?
is_perfect = true;
if S == S_est
for s=1:S
flag = false;
for s_=1:S
if M_s(s) == M_s_est(s_)
if sum(subgroup{s}==subgroup_est{s_})==M_s(s)
flag = true;
break;
end
end
end
if ~flag
is_perfect = false;
break;
end
end
else
is_perfect = false;
end
end