-
Notifications
You must be signed in to change notification settings - Fork 0
/
cdasc.m
189 lines (155 loc) · 6.36 KB
/
cdasc.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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
A = importdata('karate.txt');
truelabels = importdata('karatey.txt');
addpath('svm-matlab/common_files');
k=5;
Max_no_questions=300; % Update the number of questions, we want to ask to users
pairs = [];
pairs_prop = [];
indpo = [];
acc = [];
nbCluster=2;
nmi_all=[];
CC=[];
N=size(A,1);
% Step 1: Create the pairwise similarity matrix
display('Step 1: Compute Similarity matrix');
Sim2=zeros(N,N);
for i=1:N
for j=1:N
nij=find(A(:,i)==1 & A(:,j)==1);
ki=sum(A(:,i)==1);
kj=sum(A(:,j)==1);
%Sim(i,j)=length(nij)/(ki+kj);
Sim(i,j)=length(nij)/sqrt(ki*kj);
end
end
%replace diagonal elements with 0
display('Step 2: Find kNearestNeighbors');
[neighborIds neighborDistances] = kNearestNeighbors_mod(1-Sim, k);
%find Similarity
%Sim=-dist_mat.^2;
constraints=zeros(N,N);
%%%%%%%%%%%%%%%%%%%%%
ConnectedComp=zeros(length(N));
for i=1:N
ConnectedComp(i)=i;
end
noQuestions=0;
while noQuestions < Max_no_questions
[NcutDiscrete,output,NcutEigenvalues, U] = ncutW(Sim,nbCluster);
Outputs = zeros(N,1);
for ii = 1:nbCluster
index = find(NcutDiscrete(:,ii)==1);
Outputs(index) = ii;
end
if length(unique(Outputs))==nbCluster
nmi_index=nmi(truelabels,Outputs)
nmi_all=[nmi_all; [noQuestions nmi_index]];
end
%Compute V measure
goldla = unique(truelabels);%c
resla = unique(Outputs); %k
comatrix = zeros(length(resla),length(goldla)); %k by c
for i = 1:size(comatrix,1)
co_i = find(Outputs==resla(i));
for j = 1:size(comatrix,2)
co_j = find(truelabels == goldla(j));
c_intersect = intersect(co_i,co_j);
comatrix(i,j) = length(c_intersect);
end
end
[v,hc,hk,h_ck,h_kc] = calculate_v_measure (comatrix);
% RI = RandIndex(c1,c2)
% CRI = CRI(c1,c2,noQuestions)
% NMI = nmi(c1,c2)
% JC_now = Jaccard_Coefficient(c1);
%%%%%%
%Calculate Entropy
for ii=1:N
count = zeros(nbCluster,1);
temp = Outputs(neighborIds(:,ii));
for j = 1:length(temp)
count(temp(j)) = count(temp(j)) + 1;
end
count = count/sum(count);
entropy(ii) = 1-sum(temp==Outputs(ii))/length(temp);
end
entropy = entropy + 1e-10;
entropy(indpo) = -1;
[Y,index] = max(entropy);
indpo = [indpo;index];
while isempty(find(constraints(index,neighborIds(:,index))==0))
entropy(indpo) = -1;
[Y,index] = max(entropy);
indpo = [indpo;index];
if length(unique(entropy))==1 && unique(entropy) == -1
error('End');
break;
end
end
% Purify NN graph
pairstemp = [];
pairstemp1 = [];
for ii = 1:length(neighborIds(:,index))
if constraints(index,neighborIds(ii,index))==0
disp(['Quering: ' num2str(index) ',' num2str(neighborIds(ii,index))])
if truelabels(index) ~= truelabels(neighborIds(ii,index)) %&& ...
pairstemp = [ pairstemp;[index neighborIds(ii,index) -1 0] ];
pairstemp1 = [ pairstemp1;[index neighborIds(ii,index) -1 0] ];
%Sim(index, neighborIds(ii,index)) = -10000;
%Sim(neighborIds(ii,index), index) = -10000;
%s(y1,3)=-10000;
%s(y2,3)=-10000;
constraints(index,neighborIds(ii,index))=-1;
constraints(neighborIds(ii,index),index)=-1;
ccw=find(ConnectedComp==ConnectedComp(neighborIds(ii,index)));
constraints(index,ccw)=-1;
constraints(ccw,index)=-1;
ccw2=find(ConnectedComp==ConnectedComp(index));
constraints(ccw2,neighborIds(ii,index))=-1;
constraints(neighborIds(ii,index),ccw2)=-1;
[AMat,BMat] = meshgrid(ccw, ccw2);
constraints(AMat(:),BMat(:))=-1;
constraints(BMat(:),AMat(:))=-1;
else
pairstemp1 = [ pairstemp1;[index neighborIds(ii,index) 1 0] ];
%Sim(index, neighborIds(ii,index)) = 0;
%Sim(neighborIds(ii,index), index) = 0;
%s(y1,3)=0;
%s(y2,3)=0;
constraints(index,neighborIds(ii,index))=1;
constraints(neighborIds(ii,index),index)=1;
ccw=find(ConnectedComp==ConnectedComp(neighborIds(ii,index)));
constraints(index,ccw)=1;
constraints(ccw,index)=1;
ccw2=find(ConnectedComp==ConnectedComp(index));
constraints(ccw2,neighborIds(ii,index))=1;
constraints(neighborIds(ii,index),ccw2)=1;
[AMat,BMat] = meshgrid(ccw, ccw2);
constraints(AMat(:),BMat(:))=1;
constraints(BMat(:),AMat(:))=1;
ccw3=find(constraints(neighborIds(ii,index),:)==-1);
constraints(index,ccw3)=-1;
constraints(ccw3,index)=-1;
[AMat,BMat] = meshgrid(ccw2, ccw3);
constraints(AMat(:),BMat(:))=-1;
constraints(BMat(:),AMat(:))=-1;
ccw4=find(constraints(index,:)==-1);
constraints(neighborIds(ii,index),ccw4)=-1;
constraints(ccw4,neighborIds(ii,index))=-1;
[AMat,BMat] = meshgrid(ccw4, ccw);
constraints(AMat(:),BMat(:))=-1;
constraints(BMat(:),AMat(:))=-1;
end
noQuestions=noQuestions+1
%constraints=propConstraints_faster(constraints);
Sim=adjustSimilarity_faster_as(Sim,constraints);
Ac=constraints;
Ac(Ac<0)=0;
[numberofConnectedComp,ConnectedComp] = graphconncomp(sparse(Ac), 'Directed', false);
numberofConnectedComp
CC=[CC;[noQuestions numberofConnectedComp]];
end
end
v
end