How does it think whether three is better or four is better? It is a standard algorithm to choose the best k. It use an idea called silhouette. The silhouette value, here it is a value from -1 to 1 to determine whether a group is a good fit -- that is to say, whether the points in a certain group or a certain cluster actually belong to that cluster -- by calculating the distance between them. It is a very standard algorithm.