concept k - means in category machine learning

This is an excerpt from Manning's book Algorithms of the Intelligent Web, Second Edition.
Using a GMM to cluster can be seen as an extension of the k-means algorithm. It’s possible to achieve exactly the same results with GMM and k-means if you provide certain limitations to your GMM. We’ll discuss this in much more detail in the coming sections, but for now, you can think of GMM as a slightly more expressive model that assumes and captures some additional information about the distribution of data points. This model is also trained using the EM algorithm, and we’ll investigate and discuss how and why these two versions of EM differ slightly.
Given the Iris dataset, the basic example shown here uses k-means to see if, in the absence of any ground truth data relating to the type of the flower, we can detect any intrinsic pattern in the data. In other words, can the algorithm find any structure such that it separates the Iris dataset into three distinct clusters? Try running this now. You’ll see that, yes, the algorithm does indeed separate the data into three clusters! This is great, but you’d learn more if you could look at these clusters in comparison to the data. The next listing provides the code to visualize this.
Listing 2.5. Visualizing the output of k-means
from sklearn.cluster import KMeans from sklearn import datasets from itertools import cycle, combinations import matplotlib.pyplot as pl iris = datasets.load_iris() km = KMeans(n_clusters=3) km.fit(iris.data) predictions = km.predict(iris.data) colors = cycle('rgb') labels = ["Cluster 1","Cluster 2","Cluster 3"] targets = range(len(labels)) feature_index=range(len(iris.feature_names)) feature_names=iris.feature_names combs=combinations(feature_index,2) f,axarr=pl.subplots(3,2) axarr_flat=axarr.flat for comb, axflat in zip(combs,axarr_flat): for target, color, label in zip(targets,colors,labels): feature_index_x=comb[0] feature_index_y=comb[1] axflat.scatter(iris.data[predictions==target,feature_index_x], iris.data[predictions==target,feature_index_y],c=color,label=label) axflat.set_xlabel(feature_names[feature_index_x]) axflat.set_ylabel(feature_names[feature_index_y]) f.tight_layout() pl.show()
Figure 2.8. K-means clustering of the Iris dataset. This is achieved through the execution of listing 2.5. Note that k-means represents the centroids of the clusters in n-dimensional space, where n is the number of features (four, in the case of the Iris dataset). For visualization purposes, we plot all combinations of features against each other.
![]()
The shape of an n-dimensional Gaussian is dependent on the covariance across the dimensions in each cluster. It’s possible to obtain the same results with GMM and k-means if additional constraints are placed on the learned covariance matrices.

This is an excerpt from Manning's book Mahout in Action.
Even if the estimated centers are way off, the k-means algorithm will readjust them at the end of each iteration by computing the average center, or the centroid, of all points in the cluster. To demonstrate this corrective nature of k-means, you’ll start with center points taken close together—(1, 1) and (2, 1). You can also play with different values for the input center points and see how k-means allows various combinations of centers to converge.
Figure 9.12 shows the ellipsoidal or asymmetric distribution of points and the clusters generated by k-means. It’s clear that k-means isn’t powerful enough to figure out the distribution of these points.
Figure 9.12. Running k-means clustering over an asymmetric normal distribution of points. The points are scattered in an oval area instead of a circular one. K-means can’t generate a perfect fit for the data.
![]()
Another issue with k-means is that it requires you to estimate k, the number of seed centroids, and this value is usually overestimated. Finding the optimal value of k isn’t easy unless you have a clear understanding of the data, which happens rarely. Even by doing canopy generation, you need to tune the distance measure to make the algorithm improve the estimate of k.