0

I have tried hours but I cannot find solution.

I have "two Donuts" Data sample (variable "X")

you can download file below link

donut dataset(rings.mat)

which spreads to 2D shape like below image

First 250pts are located inside donuts and last 750 pts are located outside donuts.

2 Donut 2D sample

and I need to perform spectral clustering.

I made (similarity matrix "W") with Gaussian similarity distance.

and I made degree matrix by sum of each raw of "W"

and then I computed eigen value(E) and eigen Vector(V)

and the shape of "V" is not good.

what is wrong with my trial???

I cannot figure out.

load rings.mat
[D, N] = size(X); % data stored in X
%initial plot data
figure; hold on; 
for i=1:N,
    plot(X(1,i), X(2,i),'o');
end
% perform spectral clustering
W = zeros(N,N); 
D = zeros(N,N);

sigma = 1;
for i=1:N,
    for j=1:N,
        xixj2 = (X(1,i)-X(1,j))^2 + (X(2,i)-X(2,j))^2 ;
        W(i,j) =  exp(  -1*xixj2 / (2*sigma^2) ) ;   % compute weight here
%          if (i==j)
%              W(i,j)=0;
%          end;
    end;
     D(i,i) = sum(W(i,:))    ;
end;

L = D - W ;
normL = D^-0.5*L*D^-0.5;
[u,s,v] = svd(normL);

Image

Has QUIT--Anony-Mousse
  • 76,138
  • 12
  • 138
  • 194
MJ.Shin
  • 381
  • 1
  • 9
  • "Shape of V is not good" -- what does that mean exactly? What is the error? – dave Nov 20 '15 at 03:30
  • not an error. but I cannot find clear shape from V. can you try my code? I uploaded the dataset too. – MJ.Shin Nov 20 '15 at 03:50
  • So, following Ng, et al. you have a couple problems: 1) you didn't normalize the Laplacian. 2) You don't ever do the clustering... You can certainly take the SVD if you want, but it's probably easier just to apply k-means with k=2. Why don't you look at section 2 of this: http://ai.stanford.edu/~ang/papers/nips01-spectral.pdf – dave Nov 20 '15 at 03:54
  • Original template had SVD code for similarity matrix(W). And I have accessed This paper. But I cannot understand. If I can understand it, I will not upload this question. anyway, what should i do is calculate D^-0.5 * W * D^-0.5 ???. And what is normalization of laplacian? – MJ.Shin Nov 20 '15 at 03:59
  • So, section 2 is pretty clear, i.m.o. You created the similarity matrix successfully. You now need to: 1) take the _normalized_ laplacian instead of the unnormalized, 2) take the top eigenvectors, then 3) apply k-means to get the cluster designations. – dave Nov 20 '15 at 04:02
  • No. The normalized laplacian is D^(-1/2)*L*D^(-1/2) (https://en.wikipedia.org/wiki/Laplacian_matrix) – dave Nov 20 '15 at 04:04
  • I know that there is two version of laplacian. \n 1. one is L = D-W. two is L=D^-0.5*W*D^-0.5. what is right? what is the normalization? 3. top eigen vector should be from second minimum eigen value? or maximum value? – MJ.Shin Nov 20 '15 at 04:05
  • 1) The normalization is defined w.r.t the laplacian L, _not_ W. – dave Nov 20 '15 at 04:07
  • 2) Take the eigenvectors corresponding to the largest eigenvalues – dave Nov 20 '15 at 04:07
  • you mean (L = D-W) and (normalized L = D^(-1/2)*L*D^(-1/2) ) ??? – MJ.Shin Nov 20 '15 at 04:08
  • I calculated SVD for normalized laplacian. and applied normL to SVD. especially, most of eigenvalue from SVD was 1. some tail was less than 1. What can I found from SVD? – MJ.Shin Nov 20 '15 at 04:18
  • great. now just take the top eigenvectors and run k-means. – dave Nov 20 '15 at 04:19
  • [u,s,v] = svd(normL) outputs eigenvalue s. and more than 90% of eigen value is 1. what is wrong? – MJ.Shin Nov 20 '15 at 04:21
  • I used [u,s,v] = svd(W) and I analyzed first column of matrix "u" , and I found it is clearly clustered. – MJ.Shin Nov 20 '15 at 04:56
  • If you use the Laplacian $D^{-1/2}(D-W)D^{-1/2} = I - D^{-1/2}WD^{-1/2}$ then you will want the eigenvectors corresponding to the smallest eigenvalues. In the linked paper they suggest dropping the identity term and minus sign and using $D^{-1/2}WD^{-1/2}$. In that case you will want the eigenvectors sorresponding to the largest eigenvalues. – Nick Alger Nov 20 '15 at 09:16

1 Answers1

1

If you use the Laplacian like it is in your code (the "real" laplacian), then to cluster your points into two sets you will want the eigenvector corresponding to second smallest eigenvalue.

The intuitive idea is to connect all of your points to each other with springs, where the springs are stiffer if the points are near each other, and less stiff for points far away. The eigenvectors of the Laplacian are the modes of vibration if you hit your spring network with a hammer and watch it oscillate - smaller eigenvalues corresponding to lower frequency "bulk" modes, and larger eigenvalues corresponding to higher frequency oscillations. You want the eigenvalue corresponding to the second smallest eigenvalue, which will be like the second mode in a drum, with a positive clustered together, and negative part clustered together.

Now there is some confusion in the comments about whether to use the largest or smallest eigenvalue, and it is because the laplacian in the paper linked there by dave is slightly different, being the identity minus your laplacian. So there they want the largest ones, whereas you want the smallest. The clustering in the paper is also a bit more advanced, and better, but not as easy to implement.

Here is your code, modified to work:

load rings.mat
[D, N] = size(X); % data stored in X
%initial plot data
figure; hold on; 
for i=1:N,
    plot(X(1,i), X(2,i),'o');
end
% perform spectral clustering
W = zeros(N,N); 
D = zeros(N,N);

sigma = 0.3; % <--- Changed to be smaller
for i=1:N,
    for j=1:N,
        xixj2 = (X(1,i)-X(1,j))^2 + (X(2,i)-X(2,j))^2 ;
        W(i,j) =  exp(  -1*xixj2 / (2*sigma^2) ) ;   % compute weight here
%          if (i==j)
%              W(i,j)=0;
%          end;
end;
     D(i,i) = sum(W(i,:))    ;
end;

L = D - W ;
normL = D^-0.5*L*D^-0.5;
[u,s,v] = svd(normL);

% New code below this point
cluster1 = find(u(:,end-1) >= 0);
cluster2 = find(u(:,end-1) < 0);

figure
plot(X(1,cluster1),X(2,cluster1),'.b')
hold on
plot(X(1,cluster2),X(2,cluster2),'.r')
hold off
title(sprintf('sigma=%d',sigma))

Here is the result:

enter image description here

Now notice that I changed sigma to be smaller - from 1.0 to 0.3. When I left it at 1.0, I got the following result:

enter image description here

which I assume is because with sigma=1, the points in the inner cluster were able to "pull" on the outer cluster (which they are about distance 1 away from) enough so that it was more energetically favorable to split both circles in half like a solid vibrating drum, rather than have two different circles.

Nick Alger
  • 984
  • 7
  • 26
  • Thank you for your reply. I substitute the way to use svd(W) and get first/second eigen vector plot. And then clustering these eigen vector. using the svd(L) seems better because threshold is just 0. – MJ.Shin Nov 24 '15 at 05:52