K Means Clustering in R

So, just having a little play with clustering in R. All part of trying to develop my R skills. So here is a little demonstration of how to use K Means clustering using the Iris data.

The clusters should look like this.

Iris Answer

Start by loading the data from the built in to R.

Next we implement K Means, it’s as easy as adding 1 lines. In this example it is unsupervised learning but needs to know how many clusters it needs to find.

You can view the results using this.

Or compare the results in a table. There should be 50 of each type.

As shown in the output above, Group 1 is Versicolor, 2 is Setosa and 3 is Virginica. We can also see the the cluster incorrectly identified 14 Virginica and 2 Versicolor. But, hey, it was unsupervised so not too bad.

Next lets plot the results on a scatter graph.

Iris KMean Centers