This k-means algorithm was written from scratch in javascript.
Each of the k centroids (in this case k = numberOfPoints/groups) are randomly assigned to a point.
Then we loop the following.
The first step assigns each sample to its nearest centroid.
The second step creates new centroids by taking the mean value of all of the samples assigned to each previous centroid.
The difference between the old and the new centroids are computed and the algorithm repeats these last two steps until this value is less than a threshold.