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

11 lines
705 B
Text

NB. Selection of initial centroids, per K-means++
initialCentroids =: (] , randomCentroid)^:(<:@:]`(,:@:seedCentroid@:[))~
seedCentroid =: {~ ?@#
randomCentroid =: [ {~ [: wghtProb [: <./ distance/~
distance =: +/&.:*:@:-"1 NB. Extra credit #3 (N-dimensional is the same as 2-dimensional in J)
wghtProb =: 1&$: : ((%{:)@:(+/\)@:] I. [ ?@$ 0:)"0 1 NB. Due to Roger Hui http://j.mp/lj5Pnt
NB. Having selected the initial centroids, the standard K-means algo follows
centroids =: ([ mean/.~ closestCentroid)^:(]`_:`initialCentroids)
closestCentroid =: [: (i.<./)"1 distance/
mean =: +/ % #