-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcalc_nmi.m
66 lines (58 loc) · 1.53 KB
/
calc_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 = calc_nmi(cl1, cl2)
%CALC_NMI - Calculates NMI between two different community structures. NMI
%is a metric to measure how much two community structure resembles each
%other. See [1].
%
% Inputs:
% cl1 - First community structure
% cl2 - Second community structures
%
% Outputs:
% nmi - Calculated NMI value
%
% Other m-files required: confusion_matrix.m
% Subfunctions: none
% MAT-files required: none
%
% See also:
%
% References:
% [1] L. Danon, A. Díaz-Guilera, J. Duch, and A. Arenas, “Comparing
% community structure identification,” Journal of Statistical
% Mechanics: Theory and Experiment, vol. 2005, no. 09,
% pp. P09008–P09008, Sep. 2005, doi: 10.1088/1742-5468/2005/09/P09008.
% Author: Abdullah Karaaslanli
% Address: Michigan State University, ECE
% email: karaasl1@msu.edu
% Website: http://www.abdkarr.github.io
% Date: 17-Oct-2020; Last revision: 17-Oct-2020
%
% Copyright (c) 2020, Abdullah Karaaslanli
% All rights reserved.
C = confusion_matrix(cl1,cl2);
N = sum(sum(C));
rowsum = sum(C, 2);
colsum = sum(C, 1);
r = size(C, 1);
c = size(C, 2);
numer = 0;
for i=1:r
for j=1:c
if C(i, j) ~= 0
numer = numer+C(i,j)*log(C(i, j)*N/(rowsum(i)*colsum(j)));
end
end
end
denom = 0;
for i=1:r
if rowsum(i) ~= 0
denom = denom + rowsum(i)*log(rowsum(i)/N);
end
end
for j=1:c
if colsum(j) ~= 0
denom = denom + colsum(j)*log(colsum(j)/N);
end
end
nmi = -2*numer/(denom+eps);
end