Today saw the spectral Clustering algorithm review (mainly recently looked at the paper seems to be biased in the cluster division here), the specific look at an algorithm, the realization is also very simple, a little late, omitting the principle part (tomorrow), paste matlab code.
%%% a little overview (every time you turn to PDF 、、、)
NJW algorithm
The function u=njw (data,k)%%NJW algorithm selects the eigenvector corresponding to the first K maximum eigenvalues of the Laplace matrix, making it correspond to the original data one by one in the R (k) space, and clustering in the space%%data n*m n the number of samples M-dimension percent K before selection
The characteristic vectors corresponding to the K maximum eigenvalues are computed similar matrices affinity = calculateaffinity (data);
% Compute diagonal matrix D=eye (Size (affinity,1));
For I=1:size (affinity,1) D (i,i) = SUM (affinity (i,:)); End% computes the Laplace matrix, using the non-normalized matrix% l=d-affinity; % Normalization for i=1:size (affinity,1) for J=1:size (affinity,2) L (i,j) = affinity (i,j)/(sqrt (d (i,i)) * sqrt (d (j,j)))
;
End end% calculates eigenvalue eigenvectors [eigvectors,eigvalues] = Eig (L);
% selected before K maximum eigenvalue [eigvalues, Ind] = sort (diag (eigvalues), ' descend ');
Neigvec =eigvectors (:, Ind (1:K));
% constructs a normalized matrix U from the obtained eigenvector U=zeros (size (neigvec,1), k);
For I=1:size (neigvec,1) n = sqrt (sum (Neigvec (i,:). ^2)); U (i,:) = Neigvec (i,:).
N
End End Function [affinity] = Affinitymatrix (data) sigma = 1;
For I=1:size (data,1) a=ones (Size (data,1), 1) *data (i,:)-data;
For J=1:size (data,1) Dist=norm (A (j,:));
Affinity (I,J) = exp (-dist/(2*sigma^2));
End End End
Algorithm testing
data = rand (30,2);
Figure (1);
Plot (data (:, 1), Data (:, 2), ' r+ ');
Title (' Original Data Points ');
grid on;
U=NJW (data,3);
[Idx,c] = Kmeans (u,3);
Figure (2),
holding on;% keeps current
for i=1:size (idx,1)
if IDX (i,1) = = 1
plot (data (i,1), data (i,2), ' m+ ');
ElseIf IDX (i,1) = = 2
plot (data (i,1), data (i,2), ' g+ ');
else
plot (data (i,1), data (i,2), ' B + ');
End
end hold
off;
Title (' Clustering Results using K-means ');
Grid on;
Experimental results