RosettaCodeData/Task/K-means++-clustering/Euler-Math-Toolbox/k-means++-clustering-1.euler
Ingy döt Net db842d013d A-M baby
2013-04-10 21:29:02 -07:00

29 lines
735 B
Text

>type kmeanscluster
function kmeanscluster (x: numerical, k: index)
n=rows(x); m=cols(x);
i=floor((0:k)/k*(n-1))+1;
means=zeros(k,m);
loop 1 to k;
means[#]=sum(x[i[#]:(i[#+1]-1)]')'/(i[#+1]-i[#]);
end;
j=1:n;
loop 1 to n;
d=sum((x[#]-means)^2);
j[#]=extrema(d')[2];
end;
repeat
loop 1 to k;
i=nonzeros(j==#);
if cols(i)==0 then means[#]=1;
else means[#]=(sum(x[i]')/cols(i))';
endif;
end;
jold=j;
loop 1 to n;
d=sum((x[#]-means)^2);
j[#]=extrema(d')[2];
end;
if all(jold==j) then break; endif;
end
return j
endfunction