spectral clustering

Posted pxy7896

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了spectral clustering相关的知识,希望对你有一定的参考价值。

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的主要内容,如果未能解决你的问题,请参考以下文章

Spectral clustering(谱聚类)算法的实现

Spectral clustering(谱聚类)算法的实现

scikit-learn 中的 SpectralClustering 与 Spectral_clustering

谱聚类 Spectral Clustering

spectral clustering

Ultra-Scalable Spectral Clustering and Ensemble Clustering