# Laudari Sudip: K means in R

## 2059 days ago by bigdata2016

set.seed (3) x=matrix (rnorm (50*2) , ncol =2) x[1:25 ,1]=x[1:25 ,1]+3 x[1:25 ,2]=x[1:25 ,2] -4 km.out2 =kmeans (x,2, nstart =20) km.out2$cluster plot(x, col =(km.out2$cluster +1) , main="K-Means Clustering Results with K=2", xlab ="", ylab="", pch =20, cex =2)
  [1] 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 [49] 1 1  [1] 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 [49] 1 1
set.seed(7) x=matrix (rnorm (50*2) , ncol =2) x[1:25 ,1]=x[1:25 ,1]+3 x[1:25 ,2]=x[1:25 ,2] -4 km.out =kmeans (x,3, nstart =1) km.out$tot.withinss km.out =kmeans (x,3, nstart =20) km.out$tot.withinss km.out =kmeans (x,3, nstart =50) km.out\$tot.withinss
 [1] 72.62232 [1] 57.63156 [1] 57.63156 [1] 72.62232 [1] 57.63156 [1] 57.63156