-
Notifications
You must be signed in to change notification settings - Fork 3
/
spectral_clustering.m
202 lines (189 loc) · 5.1 KB
/
spectral_clustering.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
190
191
192
193
194
195
196
197
198
199
200
201
202
%%
% I(i) is the cluster the corresponding row i lies in
% S is the input data set, each row is a data point
% k is the cluster number
function [I] = spectral_clustering(S, k, sig)
if nargin == 3
[I] = spectral_clustering_fixed_sigma(S, k, sig);
return
elseif nargin == 2
% TODO use the 1/6 of distance range as sigma
sig_x = [0.01:0.01:0.3];
sig_x_ = [0.3:0.01:3];
sig_x = [sig_x sig_x_];
sig_y = zeros(size(sig_x));
for i = 1:size(sig_y, 2)
[~, sig_y(i)] = spectral_clustering_fixed_sigma(S, k, sig_x(i));
end
plot(sig_x, sig_y, 'b*');
sig = 0.3;
[I] = spectral_clustering_fixed_sigma(S, k, sig);
return
end
end
function [I] = golded_search(S, k)
sigma_a = 0.000001;
sigma_b = 0.15;
error = 1e-4;
%% get sigma to minimize the distortion by golded search
x1 = sigma_a + 0.382 * (sigma_b - sigma_a);
x2 = sigma_a + 0.618 * (sigma_b - sigma_a);
disp('begin');
v1 = spectral_clustering_fixed_sigma(S, k, x1);
v2 = spectral_clustering_fixed_sigma(S, k, x2);
while sigma_b - sigma_a > error
close ALL;
disp('progress');
disp([sigma_b sigma_a]);
if v1 < v2
sigma_b = x2;
x2 = x1;
v2 = v1;
x1 = sigma_a + 0.382 * (sigma_b - sigma_a);
v1 = spectral_clustering_fixed_sigma(S,k,x1);
else
sigma_a = x1;
x1 = x2;
v1 = v2;
x2 = sigma_a + 0.618 * (sigma_b - sigma_a);
v2 = spectral_clustering_fixed_sigma(S,k,x2);
end
end
sig = 0.5 * (sigma_a + sigma_b);
disp('sig');
disp(sig);
[I,d] = spectral_clustering_fixed_sigma(S, k, sig);
end
function I = spectral_clustering_from_affinity_mat(A, k)
%% 2. Get Laplacian
D = zeros(n);
for i=1:n
D(i,i) = sum(A(i,:));
end
% L = D.^(-0.5) * A * D.^(-0.5);
L = zeros(size(A));
for i=1:size(A,1)
for j=1:size(A,2)
L(i,j) = A(i,j) / (sqrt(D(i,i)) * sqrt(D(j,j)));
end
end
%% 3. Choose top K eigenvectors and Form Matrix X
[V, D] = eig(L); % *colum* of V is the eigenvectors of L
% V is already sorted in asceding order of eigenvalues
X = V(:, end-k+1 : end);
%% 4. Form Y by renormalizing X
Y = zeros(size(X));
for i=1:n
denominator = norm(X(i,:));
for j=1:k
Y(i,j) = X(i,j)/denominator;
end
end
%% 5. Clustering Y via K-means
repeat_nr = 50;
[I C] = kmeans(Y, k, 'replicates', repeat_nr);
% draw_result(I2, S);
% repeat = input('1 to repeat, 0 stop');
end
%% one pass of the algorithm
% TODO refactor this algorithm for more general use
function [I, d] = spectral_clustering_fixed_sigma(S, k, sig)
n = size(S, 1); % data set size
l = size(S, 2); % data dimension
A = zeros(n);
%% 1. Form affinity matrix A
for i=1:n
for j=1:n
if i==j
A(i,j) = 0;
else
A(i,j) = compute_Aij(S, i, j, sig);
end
end
end
%% 2. Get Laplacian
D = zeros(n);
for i=1:n
D(i,i) = sum(A(i,:));
end
% L = D.^(-0.5) * A * D.^(-0.5);
L = zeros(size(A));
for i=1:size(A,1)
for j=1:size(A,2)
L(i,j) = A(i,j) / (sqrt(D(i,i)) * sqrt(D(j,j)));
end
end
%% 3. Choose top K eigenvectors and Form Matrix X
[V, D] = eig(L); % *colum* of V is the eigenvectors of L
% V is already sorted in asceding order of eigenvalues
X = V(:, end-k+1 : end);
figure;
plot(S(:,1), S(:,2), '.');
figure;
plot(X(:, 1), '*');
figure;
plot(X(:, 2), '*');
figure;
plot(X(:,1), X(:,2), '.');
%% 4. Form Y by renormalizing X
Y = zeros(size(X));
for i=1:n
denominator = norm(X(i,:));
for j=1:k
Y(i,j) = X(i,j)/denominator;
end
end
% for debug
figure
title 'Plot of Y'
hold on
plot(Y(1:360,1), Y(1:360,2), 'b+');
plot(Y(361:720,1), Y(361:720,2), 'r*');
hold off
%% 5. Clustering Y via K-means
[I2 C] = kmeans(Y, k, 'replicates', 50);
%% for debug
%Y_1 = Y(I2==1, :);
%Y_2 = Y(I2==2, :);
%figure;
%hold on;
%plot(Y_1(:,1), Y_1(:,2), 'b+');
%plot(Y_2(:,1), Y_2(:,2), 'r*');
%hold off;
%% 6. Get I from previous result
I = I2;
%% compute distortion
d = distortion(Y, I, C);
end
function draw_result(IDX, S)
figure;
title 'Plot of Result'
hold on;
for i=1:size(IDX,1)
if IDX(i) == 1
plot(S(i,1),S(i,2),'m.');
elseif IDX(i) == 2
plot(S(i,1),S(i,2),'g+');
elseif IDX(i) == 3
plot(S(i,1),S(i,2),'b*');
elseif IDX(i) == 4
disp('ERROR: No Such Kind');
end
end
hold off;
end
%% compute distortion of a clustering
function d = distortion(S, I, C)
d = 0;
for i=1:size(I)
c = C(I(i));
d = d + norm(S(i, :)-c)^2;
end
end
%% compute A(i,j) from S
function a = compute_Aij(S, i, j, sig)
si = S(i, :);
sj = S(j, :);
t2 = -norm(si-sj).^2 / (2 * sig^2);
a = exp(t2);
end