Fully understand how K-means in the R language is clustered?

Source: Internet
Author: User

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?

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.