Skip to content

Instantly share code, notes, and snippets.

@piotrga
Created June 4, 2012 15:53
Show Gist options
  • Save piotrga/2869168 to your computer and use it in GitHub Desktop.
Save piotrga/2869168 to your computer and use it in GitHub Desktop.
KMeans in Octave
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