In this chapter, we'll go through the K-means algorithm, which is the most common clustering algorithm that you're likely to use in practice. Now, the name K-means will probably give you a little bit of intuition about how this algorithm works, but let's go through it in detail. So first, you choose a certain number of centroids. So the number of clusters that you choose is something that we will look into in a little bit more detail, but one of the things that you should recognize is that you have to actually specify the number of clusters. So you have to decide what value K takes. So is it two clusters, five clusters, 15 clusters? This is a choice that you will need to make when you are running K-means on your data. So we first start by choosing a specific number of centroids to start. Now, you can choose the placement of the centroids to be at random, or you can use a specific algorithm to decide where to start your centroids. Usually, choosing them with a specific algorithm does a better job of finding stable clusters, but you may not have the luxury of being able to run something that may take a long time on your data first. So you may simply choose them at random. Then, for each case, what we wanna do is place that case into the cluster with the closest centroid. So you look at the data in that specific case, determine which centroid it's closest to, and then link it with that specific centroid. You repeat this for every case in the data set. And then, at the end, update the centroids to be the average of the data points within each cluster. So you can see that K is the number of clusters, and then the means refers to taking the average of all of the cases to form the new centroids. After you've gone through the data set once, what you wanna do is repeat these steps continuously until the centroids stabilize and come to convergence. Here's an animation that shows the K-means algorithm in action. You can see that initially it chooses three centroids at random. Now, the blue one and the yellow one are actually quite close together. So at the beginning, the blue cluster is actually fairly small and the yellow cluster is much larger. The orange cluster, because the centroid starts quite far away from the other two centroids, is actually really large in the beginning. But as we continue to update the centroids, the orange cluster gets smaller and smaller. So this dataset is called the Mickey Mouse dataset, and you can probably figure out why. Essentially, it's made up of three blobs, one sort of larger one in the middle at the bottom, and then two smaller ones that are something that looks a little bit like Mickey Mouse and his ears. So a few things to notice about the process of running the K-means. So you can see that it does do a fairly good job of classifying the blobs that we would expect together. So the cases on the right ear are approximately together, the ones on the left ear, again, they're approximately together, and then the ones on sort of the round face are also grouped together. But in this case, there are actually a bunch of cases that probably, if we were to do it just by looking at it and deducing the pattern, we would probably put it with each of the ears. So remember this when you're thinking about the intuition around K-means, because in this case, in this dataset, what we have is clusters that have unequal variants. And the K-means algorithm really struggles with having clusters that have different variances, because when we are doing the calculation, we are assuming that the variance is equal within each of the K clusters that we've chosen. Now, this example is just in the X and Y dimensions. So there's really only two variables that it's clustering. But almost all of the clustering that you will do will be in much higher numbers of dimensions. So you may have a handful, or even a dozen, or two dozen variables that you are using in order to form your clusters. So the K-means algorithm does an analogous type function, except in higher dimensions. So instead of moving the centroids just within that X and Y plane, it's actually moving the centroid in however dimensions you have as input variables. So it could be up a few dozen variables. So there are a few reasons that K-means is the most popular algorithm. The first one is that it's relatively straightforward and quite easy to program. So we don't need very many steps in order to understand how to cluster the different rows together and how to update the center of the cluster. What that means is it's fairly straightforward to understand how the centroids are moving and to be able to interpret them when we want to understand and describe the clusters that we've created. So now you should have an intuition about how the K-means algorithm works in practice and the way that the centroids update through time, as well as the reason that this algorithm is called K-means.