...
...
...
const unClusteredData = Randomize.Array2D(
{ N: Randomize.Integer({ min: 10, max: 25 }) })
const k = Randomize.Integer(
{ min: 2, max: Math.floor(unClusteredData.length / 5) })
const recenterAndCluster = (originalClusters) => {
const centers = reCalculateCenters(originalClusters)
const clusters = cluster(unClusteredData, centers)
return { centers, clusters }
}
const improve = (loops, clusters, centers) => {
const allowImprove = () => loops < 1000
if (!allowImprove()) {
return { clusters, centers }
}
loops++
const ret = recenterAndCluster(clusters)
...
if (!allowImprove() || areCentersEqual(centers, ret.centers)) {
return ret
}
return improve(loops, ret.clusters, ret.centers)
}