The KMeans algorithm is an unsupervised clustering algorithm that seeks to partition observations into a preset number of clusters. To break that down, that means that we don't need to tell the model what groups our data falls into, but instead it tries to determine the groups itself! This is useful because we may not always have labeled data.
Looking at the graph to the left, what groups would you divide the data into? It is a little hard to tell, right? But you might be able to see one slightly distinct cluster to the upper left. Hover over the point on the graph to see the actual label to which each belongs. Did that line up with how you imagined the points would be divided?
To get started with the kmeans algorithm, we first need to decide how many groups to use. In practice this can be tricky, but for now we will cheat and decide on three groups, because we know there are three types of iris. Once we have chosen the number of groups (which we call 'k'), we will chose k random points as the centers of our clusters. These are marked in black on the graph to the right.
Then, we assign each point to belong to the group who's center is closest to it.
How does this look?
As you can see, those aren't great groupings because most of the groups have a mix of species in them. But this is just the first iteration. Next, we will recalculate the centers!
In the next step, a new center is calculated by taking the mean of all the points in a given cluster. Then, points are reassigned to the new center that they are now closest too. At the bottom of the page, you can watch this process occur step by step.
This process is repeated over and over until the cluster are no longer changing with each sucessive iteration. This means that the model has reached convergence.
On the right is the final result, once the model has reached convergence. In this case, comparing this graph to the graph above, you can see that the clusters much better match the actual clusters in the data!