concept K - means in category data

This is an excerpt from Manning's book Data Science Bookcamp: Five Python Projects.
We initialize K-means by first selecting K, which is the number of central coordinates we will search for. In our dartboard analysis, K was set to 2, though generally K can equal any whole number. The algorithm proceeds to choose K data-points at random. These data-points are treated as though they were true centers. Afterwards the algorithm iterates by updating the chosen central locations, which data scientists call centroids. During a single iteration, every data-point is assigned to its closest center. This leads to the formation of K groups. Next, the center of each group is updated. The new center equals the the mean the group’s coordinates. If we repeat the process long enough, the group-means will converge to K representative centers. The convergence is mathematically guaranteed. However, we cannot know in advance the number of iterations required for the convergence to take place. Thus, a common trick is to halt the iterations when the all of the newly computed centers do not deviate significantly from their predecessors.
Figure 10.6. The K-means algorithm iteratively converging from 2 randomly selected centroids to the actual bulls'-eye centroids.
![]()
K-means is not without its limitations. The algorithm is predicated on our knowledge of K; the number of clusters to look for. Frequently, such knowledge is not available. Also, while K-means commonly finds reasonable centers, its not mathematically guaranteed to find the best possible centers in the data. Occasionally, K-means will return non-intuitive or sub-optimal groups due to poorly selected random centroids at the initialization step of the algorithm. Finally, K-means pre-supposes that the clusters in the data actually swirl around K central locations. However, as we’ll learn later in the Section, this supposition does not always hold.
Three ring-groups are clearly present in the plot. Lets search for these 3 clusters using K-means. Obviously, we’ll set our K to 3.
Listing 10.15. Using K-means to cluster rings
rocks = [[x_coordinates[i], y_coordinates[i]] for i in range(len(x_coordinates))] rock_clusters = KMeans(3).fit_predict(rocks) colors = [['g', 'y', 'k'][cluster] for cluster in rock_clusters] plt.scatter(x_coordinates, y_coordinates, color=colors) plt.show()Figure 10.12. K-means clustering fails to properly identify the 3 distinct rock rings
![]()
The output is an utter failure! K-means dissects the data into 3 symmetric segments, and each segment spans across multiple rings. The solution doesn’t align with our intuitive expectation that each ring should fall into its own distinct group. What went wrong? Well, K-means assumed that the 3 clusters are defined by three unique centers, but the actual rings spin around a single central point. The difference between clusters is driven not by centrality, but by density. Each ring is constructed from a dense collection of points, with empty areas of sparsely populated space serving as the boundaries between rings.