K-Means clustering is one of the major clustering algorithms used to group unlabeled data points into separate clusters eg. If you have groups of malignant cells and want to know their center.
Talk is cheap, show me the code
Lets start with a simple Point and Means constructor.
Means class: it has constructor and the init() function which takes our data array and saves canvas context into ctx
init means method creates 2 random points within ranges of our data set, which are the candidates for our cluster centers
bLarge assign method which gets 2 cluster centers and fills in distances array with objects containing distances between each point and cluster center. Then, we change assignTo property according to which centroid is closer to that point (0 or value) (index of the centroid in the means array)
We than take all indexes of the points assigned to 0 index get that point and change its color to orange and push data to data array, same for blue points (index 1)
Get the mean of both clusters (points assigned to the orange cluster are at the first half, points assigned to the 2nd cluster are at the 2nd half, we return means of both halves
Plot the points on our canvas.
This is where the magic happens: 1) assign data points to centroid or centroid according to distance and get their center 2) assign all points to that new center position and get their mean position again
Usage: <canvas width="400" height="400"> is needed. Call m.try() repeatedly to try to cluster points and see the results in console.