Spectral clustering (NJW algorithm matlab code)

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.