-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathmyClustMeasure.m
54 lines (51 loc) · 2.01 KB
/
myClustMeasure.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
function [jm,purity] =myClustMeasure(calculatedClust, realClust,emptyRealMapping)
% Initialization
efficiency = 0; purity = 0; PE = 0; jm = 0;
if nargin <3 || emptyRealMapping ==0
pNum=length(calculatedClust);
% both vectors should be in length*1 format
[x,y] = size(calculatedClust);
if y>x
calculatedClust = calculatedClust';
end
[x,y] = size(realClust);
if y >x
realClust=realClust';
end
if 2 ~= length (find(size(calculatedClust) == size (realClust)))
errordlg('Dimensions of Calculated and real mapping are mismatch','Wrong dimensions','modal');
return
end
% S=the clutering result in pairs - S(i,j)=1 if data point i and j are asigned to the same cluster
clustPairs = establishPairsMatrix (calculatedClust);
realClustPairs=establishPairsMatrix (realClust);
n11Mat= clustPairs&realClustPairs; %pairs that appear in both methods
n11=length(find(n11Mat)); % number of pairs that appear in both methods
n10 = length(find(realClustPairs)) - n11; % number of pairs that appear in 'real' classification, but not in the algorithm
n01 =length(find(clustPairs)) - n11; % number of pairs that appear in algorithm, but not in the 'real' classification
if (n10+n01+n11)>0
jm=n11/(n10+n01+n11);
end
if (n10+n11)>0
%efficiency=sum(sum(efficiencyMat))/(numOfRealClust*numOfCalcClust);
efficiency = n11/(n10+n11);
end
if (n01+n11)>0
%purity=sum(sum(purityMat))/(numOfRealClust*numOfCalcClust);
purity = n11/(n01+n11);
end
PE=sqrt(efficiency^2+purity^2);%norm
end
function [pairsMatrix]= establishPairsMatrix (array)
len = length(array);
pairsMatrix = sparse(len,len);
for ind = 1:max(array)
[indices]= find(array==ind);
lenInd = length(indices);
if mod(ind,10) ==0
disp('.');
end
for jnd = 1:lenInd-1
pairsMatrix(indices(jnd),indices(jnd+1:lenInd))=1;
end
end