-
Notifications
You must be signed in to change notification settings - Fork 29
/
Copy pathspectral_clustering.m
29 lines (23 loc) · 1.07 KB
/
spectral_clustering.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
%--------------------------------------------------------------------------
% This function takes an adjacency matrix of a graph and computes the
% clustering of the nodes using the spectral clustering algorithm of
% Ng, Jordan and Weiss.
% CMat: NxN adjacency matrix
% n: number of groups for clustering
% groups: N-dimensional vector containing the memberships of the N points
% to the n groups obtained by spectral clustering
%--------------------------------------------------------------------------
% Copyright @ Ehsan Elhamifar, 2012
% Modified @ Chong You, 2015
%--------------------------------------------------------------------------
function [CA F P R nmi AR] = spectral_clustering(A,k,truth)
N = size(A,1);
% Normalized spectral clustering according to Ng & Jordan & Weiss
% using Normalized Symmetric Laplacian L = I - D^{-1/2} W D^{-1/2}
DN = diag( 1./sqrt(sum(A)+eps) );
LapN = speye(N) - DN * A * DN;
[~,~,vN] = svd(LapN);
kerN = vN(:,N-k+1:N);
normN = sum(kerN .^2, 2) .^.5;
kerNS = bsxfun(@rdivide, kerN, normN + eps);
[CA F P R nmi AR] = performance_kmeans(kerNS,k,truth);