The process of K-means clustering is demonstrated below on the iris dataset.
First remove the species property from the iris dataset, then call the function Kmeans on the dataset Iris and store the cluster results in a variable kmeans.result.
In the following code, the number of clusters is set to 3.
Iris2 <-Iris
Iris2$species <-NULL
(Kmeans.result <-Kmeans (Iris2, 3))
Compare cluster results to class labels (species) to see if similar objects are divided into the same cluster.
Table (Iris$species, Kmeans.result$cluster)
From the clustering results above, it can be seen that the Setosa class is easily separated from the other two classes, and there is a small overlap between the versicolor class and the Virginica class.
K-means Clustering Results diagram
Then, draw all the clusters and the center of the cluster.
Note that the dataset has 4 dimensions, and the drawing uses only the first two dimensions of data.
650) this.width=650; "Src=" Https://s1.51cto.com/oss/201711/09/8457f970e2d95785616c11293da2311d.jpg-wh_500x0-wm_3 -wmp_4-s_3470763792.jpg "title=" Untitled 1.jpg "alt=" 8457f970e2d95785616c11293da2311d.jpg-wh_ "/>
Some of the black dots shown in the figure that are close to the green Center (marked with an asterisk) are actually closer to the black center in 4-dimensional space.
It is important to note that the K-means clustering results can be different for multiple runs, because the initial cluster centers are randomly selected.
Plot (Iris2[c ("Sepal.length", "Sepal.width")], col = kmeans.result$cluster)
# Plot Cluster centers
Points (Kmeans.result$centers[,c ("Sepal.length", "Sepal.width")], col = 1:3, pch = 8, cex=2)
This article from the "CAS Computer Training" blog, declined to reprint!
Fully understand how K-means in the R language is clustered?