8.4 Clustering | K-means algorithm-- [Machine Learning | Andrew Ng]
 In the last video you saw an illustration of the k-means algorithm running. Now let's write out the k-means algorithm in detail so that you'd be able to implement it for yourself. Here's the k-means algorithm. The first step is to randomly initialize k cluster centroid mu1, mu2 through mu k. In the example that we had, this corresponded to when we randomly chose a location for the red cross and for the blue cross corresponding to the two cluster centroids. In our example k was equal to 2, so if the red cross was cluster centroid 1 and the blue cross was cluster centroid 2, these are just two indices to denote the first and the second cluster, then the red cross would be the location of mu1 and the blue cross would be the location of mu2. And just to be clear, mu1 and mu2 are vectors which have the same dimension as your training examples x1 through say x30 in our example. So all of these are lists of two numbers or they are two dimensional vectors or whatever dimension the training data had. So we had n equals two features for each of the training examples, then mu1 and mu2 will also be two dimensional vectors, meaning vectors with two numbers in them. Having randomly initialized the k cluster centroids, k-means will then repeatedly carry out the two steps that you saw in the last video. The first step is to assign points to cluster centroids, meaning color each of the points either red or blue corresponding to assigning them to cluster centroids 1 or 2 when k is equal to 2. Rinse it out in math, that means that we're going to for i equals 1 through m, for our m training examples, we're going to set ci to be equal to the index, which can be anything from 1 to k of the cluster centroid closest to the training example xi. Mathematically, you can write this out as computing the distance between xi and mu k. And in math, the distance between two points is often written like this. It is also called the L2 norm, and what you want to find is the value of k that minimizes this because that corresponds to the cluster centroid mu k that is closest to the training example xi. And then the value of k that minimizes this is what gets set to ci. When you implement this algorithm, you find that it's actually a little bit more convenient to minimize the squared distance because the cluster centroid with the smallest square distance should be the same as the cluster centroid with the smallest distance. And when you look at this week's optional labs and practice labs, you see how to implement this in code for yourself. As a concrete example, this point up here is closer to the red or to cluster centroid 1. So if this was training example x1, we would set c1 to be equal to 1. Whereas this point over here, if this was the 12 training example, this is closer to the second cluster centroid, the blue one. And so we will set this, the corresponding cluster assignment variable to 2 because it's closer to cluster centroid 2. So that's the first step of the k-means algorithm, assign points to cluster centroids. The second step is to move the cluster centroids. And what that means is for lowercase k equals 1 to capital K, the number of clusters, we're going to set the cluster centroids location to be updated to be the average or the mean of the points assigned to that cluster k. Concretely, what that means is we'll look at all of these red points, say, and look at their position on the horizontal axis, look at the value of the first feature x1 and average that out and compute the average value on the vertical axis as well. And after computing those two averages, you find that the mean is here, which is why mu 1, that is the location of the red cluster centroid, gets updated as follows. Similarly, we would look at all of the points that were colored blue, that is, with ci equals 2 and compute the average of the value on the horizontal axis, the average of their feature x1, compute the average of the feature x2, and those two averages give you the new location of the blue cluster centroid, which therefore moves over here. Just to write those out in math, if the first cluster had assigned to it training examples 1, 5, 6, and 10, just as an example, then what that means is you would compute the average this way, notice that x1, x5, x6, and x10 are training examples, four training examples, so we divide by four, and this gives you the new location of mu 1, the new cluster centroid for cluster 1. To be clear, each of these x values are vectors with two numbers in them or n numbers in them if you have n features, and so mu will also have two numbers in it or n numbers in it if you have n features instead of two. Now there is one corner case to this algorithm, which is what happens if a cluster has zero training examples assigned to it? In that case, the second step mu k would be trying to compute the average of zero points, and that's not well defined. If that ever happens, the most common thing to do is to just eliminate that cluster so you end up with k minus 1 clusters, or if you really, really need k clusters, an alternative would be to just randomly reinitialize that cluster centroid and hope that it gets assigned at least some points next time around, but it's actually more common when running k means to just eliminate a cluster if no points are assigned to it. Even though I've mainly been describing k means for clusters that are well separated, so clusters that may look like this, where if you ask it to find three clusters, hopefully it will find these three distinct clusters, it turns out that k means is also frequently applied to datasets where the clusters are not that well separated. For example, if you are a designer and manufacturer of cool t-shirts and you want to decide if how do I size my small, medium, and large t-shirts? How small should a small be? How large should a large be? And what should a medium-sized t-shirt really be? And so one thing you might do is collect data of people likely to buy your t-shirts based on their heights and weights, and you find that the height and weight of people tend to vary continuously on the spectrum without very clear clusters. Nonetheless, if you were to run k means with, say, three cluster centroids, you might find that k means would group these points into one cluster, these points into a second cluster, and these points into a third cluster. And so if you're trying to decide exactly how to size your small, medium, and large t-shirts, you might then choose the dimensions of your small t-shirt to try to make it fit these individuals well, the medium-sized t-shirt to try to fit these individuals well, and the large t-shirt to try to fit these individuals well, with potentially the cluster centroids giving you a sense of what is the most representative height and weight that you would want your three t-shirt sizes to fit. So this is an example of k means working just fine and giving a useful result, even if the data does not lie in well-separated groups or clusters. So that was the k means clustering algorithm. Assign cluster centroids randomly and then repeatedly assign points to cluster centroids and move the cluster centroids. But what is this algorithm really doing? And do we think this algorithm will converge or might it just keep on running forever and never converge? To gain deeper intuition about the k means algorithm and also see why we might hope this algorithm does converge, let's go on to the next video where you see that k means is actually trying to optimize a specific cost function. Let's take a look at that in the next video.
[{"start": 0.0, "end": 7.32, "text": " In the last video you saw an illustration of the k-means algorithm running."}, {"start": 7.32, "end": 11.64, "text": " Now let's write out the k-means algorithm in detail so that you'd be able to implement"}, {"start": 11.64, "end": 12.64, "text": " it for yourself."}, {"start": 12.64, "end": 15.32, "text": " Here's the k-means algorithm."}, {"start": 15.32, "end": 24.5, "text": " The first step is to randomly initialize k cluster centroid mu1, mu2 through mu k."}, {"start": 24.5, "end": 31.12, "text": " In the example that we had, this corresponded to when we randomly chose a location for the"}, {"start": 31.12, "end": 39.36, "text": " red cross and for the blue cross corresponding to the two cluster centroids."}, {"start": 39.36, "end": 46.44, "text": " In our example k was equal to 2, so if the red cross was cluster centroid 1 and the blue"}, {"start": 46.44, "end": 53.08, "text": " cross was cluster centroid 2, these are just two indices to denote the first and the second"}, {"start": 53.08, "end": 62.519999999999996, "text": " cluster, then the red cross would be the location of mu1 and the blue cross would be the location"}, {"start": 62.519999999999996, "end": 64.92, "text": " of mu2."}, {"start": 64.92, "end": 72.64, "text": " And just to be clear, mu1 and mu2 are vectors which have the same dimension as your training"}, {"start": 72.64, "end": 77.44, "text": " examples x1 through say x30 in our example."}, {"start": 77.44, "end": 83.7, "text": " So all of these are lists of two numbers or they are two dimensional vectors or whatever"}, {"start": 83.7, "end": 86.92, "text": " dimension the training data had."}, {"start": 86.92, "end": 93.6, "text": " So we had n equals two features for each of the training examples, then mu1 and mu2 will"}, {"start": 93.6, "end": 99.2, "text": " also be two dimensional vectors, meaning vectors with two numbers in them."}, {"start": 99.2, "end": 105.86, "text": " Having randomly initialized the k cluster centroids, k-means will then repeatedly carry"}, {"start": 105.86, "end": 109.34, "text": " out the two steps that you saw in the last video."}, {"start": 109.34, "end": 114.76, "text": " The first step is to assign points to cluster centroids, meaning color each of the points"}, {"start": 114.76, "end": 124.1, "text": " either red or blue corresponding to assigning them to cluster centroids 1 or 2 when k is"}, {"start": 124.1, "end": 126.72, "text": " equal to 2."}, {"start": 126.72, "end": 132.96, "text": " Rinse it out in math, that means that we're going to for i equals 1 through m, for our"}, {"start": 132.96, "end": 139.28, "text": " m training examples, we're going to set ci to be equal to the index, which can be anything"}, {"start": 139.28, "end": 145.36, "text": " from 1 to k of the cluster centroid closest to the training example xi."}, {"start": 145.36, "end": 152.88, "text": " Mathematically, you can write this out as computing the distance between xi and mu k."}, {"start": 152.88, "end": 158.60000000000002, "text": " And in math, the distance between two points is often written like this."}, {"start": 158.6, "end": 167.0, "text": " It is also called the L2 norm, and what you want to find is the value of k that minimizes"}, {"start": 167.0, "end": 176.07999999999998, "text": " this because that corresponds to the cluster centroid mu k that is closest to the training"}, {"start": 176.07999999999998, "end": 179.28, "text": " example xi."}, {"start": 179.28, "end": 187.72, "text": " And then the value of k that minimizes this is what gets set to ci."}, {"start": 187.72, "end": 192.24, "text": " When you implement this algorithm, you find that it's actually a little bit more convenient"}, {"start": 192.24, "end": 198.68, "text": " to minimize the squared distance because the cluster centroid with the smallest square"}, {"start": 198.68, "end": 205.92, "text": " distance should be the same as the cluster centroid with the smallest distance."}, {"start": 205.92, "end": 211.4, "text": " And when you look at this week's optional labs and practice labs, you see how to implement"}, {"start": 211.4, "end": 214.07999999999998, "text": " this in code for yourself."}, {"start": 214.08, "end": 220.76000000000002, "text": " As a concrete example, this point up here is closer to the red or to cluster centroid"}, {"start": 220.76000000000002, "end": 221.76000000000002, "text": " 1."}, {"start": 221.76000000000002, "end": 229.72000000000003, "text": " So if this was training example x1, we would set c1 to be equal to 1."}, {"start": 229.72000000000003, "end": 234.44, "text": " Whereas this point over here, if this was the 12 training example, this is closer to"}, {"start": 234.44, "end": 237.24, "text": " the second cluster centroid, the blue one."}, {"start": 237.24, "end": 243.84, "text": " And so we will set this, the corresponding cluster assignment variable to 2 because it's"}, {"start": 243.84, "end": 246.52, "text": " closer to cluster centroid 2."}, {"start": 246.52, "end": 253.0, "text": " So that's the first step of the k-means algorithm, assign points to cluster centroids."}, {"start": 253.0, "end": 257.64, "text": " The second step is to move the cluster centroids."}, {"start": 257.64, "end": 264.72, "text": " And what that means is for lowercase k equals 1 to capital K, the number of clusters, we're"}, {"start": 264.72, "end": 272.74, "text": " going to set the cluster centroids location to be updated to be the average or the mean"}, {"start": 272.74, "end": 275.44, "text": " of the points assigned to that cluster k."}, {"start": 275.44, "end": 280.68, "text": " Concretely, what that means is we'll look at all of these red points, say, and look"}, {"start": 280.68, "end": 286.40000000000003, "text": " at their position on the horizontal axis, look at the value of the first feature x1"}, {"start": 286.40000000000003, "end": 292.66, "text": " and average that out and compute the average value on the vertical axis as well."}, {"start": 292.66, "end": 299.44, "text": " And after computing those two averages, you find that the mean is here, which is why mu"}, {"start": 299.44, "end": 306.04, "text": " 1, that is the location of the red cluster centroid, gets updated as follows."}, {"start": 306.04, "end": 313.76, "text": " Similarly, we would look at all of the points that were colored blue, that is, with ci equals"}, {"start": 313.76, "end": 320.84, "text": " 2 and compute the average of the value on the horizontal axis, the average of their"}, {"start": 320.84, "end": 327.36, "text": " feature x1, compute the average of the feature x2, and those two averages give you the new"}, {"start": 327.36, "end": 333.48, "text": " location of the blue cluster centroid, which therefore moves over here."}, {"start": 333.48, "end": 341.32, "text": " Just to write those out in math, if the first cluster had assigned to it training examples"}, {"start": 341.32, "end": 353.28000000000003, "text": " 1, 5, 6, and 10, just as an example, then what that means is you would compute the average"}, {"start": 353.28, "end": 362.52, "text": " this way, notice that x1, x5, x6, and x10 are training examples, four training examples,"}, {"start": 362.52, "end": 369.55999999999995, "text": " so we divide by four, and this gives you the new location of mu 1, the new cluster centroid"}, {"start": 369.55999999999995, "end": 372.15999999999997, "text": " for cluster 1."}, {"start": 372.15999999999997, "end": 379.88, "text": " To be clear, each of these x values are vectors with two numbers in them or n numbers in them"}, {"start": 379.88, "end": 386.48, "text": " if you have n features, and so mu will also have two numbers in it or n numbers in it"}, {"start": 386.48, "end": 389.96, "text": " if you have n features instead of two."}, {"start": 389.96, "end": 397.04, "text": " Now there is one corner case to this algorithm, which is what happens if a cluster has zero"}, {"start": 397.04, "end": 399.0, "text": " training examples assigned to it?"}, {"start": 399.0, "end": 405.24, "text": " In that case, the second step mu k would be trying to compute the average of zero points,"}, {"start": 405.24, "end": 407.46, "text": " and that's not well defined."}, {"start": 407.46, "end": 412.4, "text": " If that ever happens, the most common thing to do is to just eliminate that cluster so"}, {"start": 412.4, "end": 419.28, "text": " you end up with k minus 1 clusters, or if you really, really need k clusters, an alternative"}, {"start": 419.28, "end": 424.84, "text": " would be to just randomly reinitialize that cluster centroid and hope that it gets assigned"}, {"start": 424.84, "end": 429.67999999999995, "text": " at least some points next time around, but it's actually more common when running k means"}, {"start": 429.67999999999995, "end": 434.74, "text": " to just eliminate a cluster if no points are assigned to it."}, {"start": 434.74, "end": 439.52, "text": " Even though I've mainly been describing k means for clusters that are well separated,"}, {"start": 439.52, "end": 446.04, "text": " so clusters that may look like this, where if you ask it to find three clusters, hopefully"}, {"start": 446.04, "end": 451.88, "text": " it will find these three distinct clusters, it turns out that k means is also frequently"}, {"start": 451.88, "end": 456.88, "text": " applied to datasets where the clusters are not that well separated."}, {"start": 456.88, "end": 464.72, "text": " For example, if you are a designer and manufacturer of cool t-shirts and you want to decide if"}, {"start": 464.72, "end": 470.16, "text": " how do I size my small, medium, and large t-shirts?"}, {"start": 470.16, "end": 471.52000000000004, "text": " How small should a small be?"}, {"start": 471.52000000000004, "end": 472.76000000000005, "text": " How large should a large be?"}, {"start": 472.76000000000005, "end": 476.12, "text": " And what should a medium-sized t-shirt really be?"}, {"start": 476.12, "end": 481.28000000000003, "text": " And so one thing you might do is collect data of people likely to buy your t-shirts based"}, {"start": 481.28000000000003, "end": 487.52000000000004, "text": " on their heights and weights, and you find that the height and weight of people tend"}, {"start": 487.52000000000004, "end": 492.44000000000005, "text": " to vary continuously on the spectrum without very clear clusters."}, {"start": 492.44, "end": 500.2, "text": " Nonetheless, if you were to run k means with, say, three cluster centroids, you might find"}, {"start": 500.2, "end": 506.4, "text": " that k means would group these points into one cluster, these points into a second cluster,"}, {"start": 506.4, "end": 509.72, "text": " and these points into a third cluster."}, {"start": 509.72, "end": 515.68, "text": " And so if you're trying to decide exactly how to size your small, medium, and large"}, {"start": 515.68, "end": 522.36, "text": " t-shirts, you might then choose the dimensions of your small t-shirt to try to make it fit"}, {"start": 522.36, "end": 528.36, "text": " these individuals well, the medium-sized t-shirt to try to fit these individuals well, and"}, {"start": 528.36, "end": 535.36, "text": " the large t-shirt to try to fit these individuals well, with potentially the cluster centroids"}, {"start": 535.36, "end": 540.2, "text": " giving you a sense of what is the most representative height and weight that you would want your"}, {"start": 540.2, "end": 543.72, "text": " three t-shirt sizes to fit."}, {"start": 543.72, "end": 550.2, "text": " So this is an example of k means working just fine and giving a useful result, even if the"}, {"start": 550.2, "end": 555.08, "text": " data does not lie in well-separated groups or clusters."}, {"start": 555.08, "end": 558.6800000000001, "text": " So that was the k means clustering algorithm."}, {"start": 558.6800000000001, "end": 563.32, "text": " Assign cluster centroids randomly and then repeatedly assign points to cluster centroids"}, {"start": 563.32, "end": 566.0400000000001, "text": " and move the cluster centroids."}, {"start": 566.0400000000001, "end": 568.36, "text": " But what is this algorithm really doing?"}, {"start": 568.36, "end": 572.08, "text": " And do we think this algorithm will converge or might it just keep on running forever and"}, {"start": 572.08, "end": 573.44, "text": " never converge?"}, {"start": 573.44, "end": 578.76, "text": " To gain deeper intuition about the k means algorithm and also see why we might hope this"}, {"start": 578.76, "end": 584.36, "text": " algorithm does converge, let's go on to the next video where you see that k means is actually"}, {"start": 584.36, "end": 587.84, "text": " trying to optimize a specific cost function."}, {"start": 587.84, "end": 610.0, "text": " Let's take a look at that in the next video."}]