Created
June 4, 2012 15:53
-
-
Save piotrga/2869168 to your computer and use it in GitHub Desktop.
KMeans in Octave
This file contains 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
function [cent, idx, sizes, distortion]=km(data, k, maxIter) | |
N = size(data,1); | |
COL = size(data,2); | |
cent = data(randperm(N)(1:k), : ); | |
#cent = rand(k, COL) * 0.5 + 0.5 | |
for iter = 1 : maxIter | |
fprintf("Iteration %d\n", iter); | |
dist = []; | |
for i = 1 : k | |
dist = [dist ; sumsq( (data - repmat(cent(i,:),N, 1))')]; | |
endfor | |
[val, idx] = min(dist); | |
cent = []; | |
for i = 1: k | |
cent = [cent; mean(data(idx == i,:) ,1)]; | |
endfor | |
sizes = []; | |
for i = 1 : k | |
sizes = [sizes; length(idx(idx == i))]; | |
endfor | |
sizes' | |
endfor | |
distortion = 0; | |
for i = 1 : k | |
distortion = distortion + sum(sumsq((data(idx == i, :) - repmat(cent(i,:), sizes(i), 1)) )) / sizes(i); | |
endfor | |
distortion |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment