I am attempting to implement a spectral clustering routine using Mathematica, but I am encountering difficulties.
Using the standard Gaussian similarity function found in section 2.2 of this paper and setting the parameter $\sigma$ to 1, I have constructed the following code:
SpectralCluster[data_, k_, ε_] :=
Module[{Similarity, Weight, Walk, LRW, Eigenvals, Eigenvecs, Kvecs},
Similarity= With[{tr = Transpose[data]},
Function[point, Exp[-Sqrt[Total[(point-tr)^2]]/ε]] /@data];
Weight = DiagonalMatrix[Total /@ Similarity];
Walk = Inverse[Weight].Similarity;
LRW = IdentityMatrix[Length[Similarity]] - Walk;
{Eigenvals, Eigenvecs} = Eigensystem[LRW];
Kvecs = Take[Eigenvecs, k];
ClusteringComponents[Kvecs, k, 1, Method ->"KMeans",
DistanceFunction->SquaredEuclideanDistance]
]
To test the algorithm I generate the following data:
x1=RandomVariate[MultinormalDistribution[{2, 0},{{1, 0},{0, 8}}],25];
x2=RandomVariate[MultinormalDistribution[{4, 0},{{1, 0},{0, 8}}],25];
x3=RandomVariate[MultinormalDistribution[{6, 0},{{1, 0},{0, 8}}],25];
x4=RandomVariate[MultinormalDistribution[{8, 0},{{1, 0},{0, 8}}],25];
pts=Join[x1,x2,x3,x4];
ListPlot[pts]
Given the following inputs
SpecCluster = SpectralCluster[pts, 4, 1]
ListPlot[{pts, SpecCluster}]
I obtain the outputs
{1,2,3,4}
I've run my spectral clustering code on a few different sets of data (not always generated from the Multinormal Distribution) and it always returns a set of increasing integers.
Where am I going wrong?