码迷,mamicode.com
首页 > 其他好文 > 详细

spectral clustering

时间:2017-03-06 11:05:38      阅读:285      评论:0      收藏:0      [点我收藏+]

标签:cto   end   size   function   spec   data-   sorted   eigen   hold   

A = load(‘Gaussian.txt‘);
%A = load(‘ringData.txt‘);
[m, n] = size(A);
data=A(:,:);
% markersize default is 6
%plot(data(:,1),data(:,2),‘.‘, ‘markersize‘, 10);
% col sum. x^2 + y^2
squareData = sum(data.*data,2);
transitData = data*data‘;
% repmat means repeat matrix to 1*m
xi = repmat(squareData,1,m);
xj = repmat(squareData‘,m,1);
E = 2*transitData-xi-xj;
sig = 1;
W = exp(E / (2 * sig * sig));
% set diagnoal to zeros
W = W - diag(diag(W));
D = diag(sum(W,2));
%L = D-W;
L =D^(-.5)*W*D^(-.5);

K = 3;
[X, di] = eig(L);
[Xsort, Dsort] = eigsort(X, di);
%Xsort = sort(X);
Xuse = Xsort(:, 1:K);

Xsq = Xuse.*Xuse;
divmat=repmat(sqrt(sum(Xsq‘)‘),1,K);
Y=Xuse./divmat;

[c, Dsum, z] = kmeans(Y, K);
kk = c;
c1 = find(kk == 1);
c2 = find(kk == 2);
c3 = find(kk == 3);

plot(data(c1,1),data(c1,2),‘.‘,‘marker‘, ‘+‘);
hold on;
plot(data(c2,1),data(c2,2),‘.‘,‘marker‘, ‘*‘);
hold on;
plot(data(c3,1),data(c3,2),‘.‘,‘marker‘, ‘+‘);

function [Vsort, Dsort] = eigsort (V, D)
  eigvals = diag(D);
  [lohival,lohiindex] = sort(eigvals);
  lambda = flipud(lohival);
  index = flipud(lohiindex);
  Dsort = diag(lambda);
% Sort eigenvectors to correspond to the ordered eigenvalues. Store sorted
% eigenvectors as columns of the matrix vsort.
  M = length(lambda);
  Vsort = zeros(M,M);
for i=1:M
  Vsort(:,i) = V(:,index(i));
end;

  

spectral clustering

标签:cto   end   size   function   spec   data-   sorted   eigen   hold   

原文地址:http://www.cnblogs.com/pxy7896/p/6508670.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!