Created
December 17, 2014 12:56
-
-
Save cyhsutw/b392899e7f3f79659e11 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| classdef KmeansClustering < handle | |
| properties | |
| X | |
| k | |
| seeds | |
| indicators | |
| end | |
| methods | |
| function objInstance = KmeansClustering(X, k) | |
| %objInstance = KmeansClustering(); | |
| objInstance.X = X; | |
| objInstance.k = k; | |
| end | |
| function indicators = KmeansCluster(obj) | |
| [m, n] = size(obj.X); | |
| labels = zeros(m, obj.k); | |
| seeds = obj.KmeansPlusPlusInit; | |
| while(1) | |
| labels = zeros(m, obj.k); | |
| for i=1:m | |
| dist = inf; | |
| label = 0; | |
| instance = obj.X(i,:); | |
| for j=1:obj.k | |
| seed = seeds(j,:); | |
| nDist = pdist2(seed, instance); | |
| if dist > nDist | |
| label = j; | |
| dist = nDist; | |
| end | |
| end | |
| labels(i, label) = 1; | |
| end | |
| newSeeds = zeros(obj.k, n); | |
| for i=1:obj.k | |
| group = labels(:,i); | |
| groupSize = 0; | |
| for j=1:m | |
| if group(j) == 1 | |
| newSeeds(i,:) = newSeeds(i,:) + obj.X(j,:); | |
| groupSize = groupSize + 1; | |
| end | |
| end | |
| newSeeds(i,:) = newSeeds(i,:)./groupSize; | |
| end | |
| if isequal(newSeeds, seeds) | |
| break; | |
| end | |
| seeds = newSeeds; | |
| end | |
| obj.indicators = labels; | |
| end | |
| function seeds = KmeansPlusPlusInit(obj) | |
| [m, n] = size(obj.X); | |
| seeds = zeros(obj.k, n); | |
| seedSet = zeros(obj.k, 1); | |
| seedSet(1) = randi(m); | |
| seeds(1,:) = obj.X(seedSet(1),:); | |
| numSeeds = 1; | |
| while(1) | |
| farest = 0; | |
| newSeed = 0; | |
| base = obj.X(seedSet(numSeeds),:); | |
| for i = 1:m | |
| if sum(ismember(seedSet, i)) == 0 | |
| dist = pdist2(base, obj.X(i,:)); | |
| if dist > farest | |
| newSeed = i; | |
| farest = dist; | |
| end | |
| end | |
| end | |
| numSeeds = numSeeds + 1; | |
| seedSet(numSeeds) = newSeed; | |
| seeds(numSeeds,:) = obj.X(newSeed,:); | |
| if numSeeds == obj.k | |
| break; | |
| end | |
| end | |
| end | |
| end | |
| methods (Static) | |
| function clusterIndicators = cluster(X, k) | |
| obj = model.clustering.KmeansClustering(X, k); | |
| obj.KmeansCluster(); | |
| clusterIndicators = obj.indicators; | |
| obj.seeds | |
| end | |
| end | |
| end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| classdef SpectralClustering < model.clustering.KmeansClustering | |
| properties | |
| cfg | |
| end | |
| methods | |
| function objInstance = SpectralClustering(X, k, cfg) | |
| objInstance@model.clustering.KmeansClustering(X, k); | |
| objInstance.X = X; | |
| objInstance.k = k; | |
| objInstance.cfg = cfg; | |
| end | |
| function spectralCluster(obj) | |
| [m, n] = size(obj.X); | |
| [l, d, s] = obj.buildLaplacian(); | |
| [v, something] = eigs(l, d, obj.k, 'SM'); | |
| u = zeros(m, obj.k); | |
| for i=1:obj.k | |
| u(:,i) = v(:,i); | |
| end | |
| obj.X = u; | |
| obj.KmeansCluster(); | |
| end | |
| function [l, d, s] = buildLaplacian(obj) | |
| sim = obj.cfg('similarity'); | |
| [m, n] = size(obj.X); | |
| s = zeros(m, m); | |
| if (strcmp(sim, 'eNN')) | |
| s = obj.buildENNLaplacian(); | |
| elseif (strcmp(sim, 'eBall')) | |
| s = obj.buildEBallLaplacian(); | |
| elseif (strcmp(sim, 'Gaussian')) | |
| s = obj.buildGaussianLaplacian(); | |
| end | |
| d = zeros(m, m); | |
| for i=1:m | |
| d(i, i) = sum(s(i,:)); | |
| end | |
| l = d - s; | |
| end | |
| function s = buildENNLaplacian(obj) | |
| e = obj.cfg('e'); | |
| [m, n] = size(obj.X); | |
| s = zeros(m, m); | |
| for i=1:m | |
| idx = knnsearch(obj.X, obj.X(i,:), 'k', e+1); | |
| for j=1:e+1 | |
| if idx(j) ~= i | |
| s(i, j) = 1; | |
| end | |
| end | |
| end | |
| end | |
| function s = buildEBallLaplacian(obj) | |
| e = obj.cfg('e'); | |
| [m, n] = size(obj.X); | |
| end | |
| function s = buildGaussianLaplacian(obj) | |
| end | |
| end | |
| methods (Static) | |
| function clusterIndicators = cluster(X, k, cfg) | |
| obj = model.clustering.SpectralClustering(X, k, cfg); | |
| obj.spectralCluster(); | |
| clusterIndicators = obj.indicators; | |
| end | |
| end | |
| end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment