# k-means

## 622 days ago by Buyan

Example of K-Means clustering for k=2

set.seed (2) 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,2, nstart =20) km.out$cluster plot(x, col =(km.out$cluster +1) , main="K-Means Clustering Results with K=2", xlab ="", ylab="", pch =20, cex =2) plot(x, col =(km.out$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 Example of K-Means clustering for k=3 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.out =kmeans (x,3, nstart =20) km.out$cluster plot(x, col =(km.out$cluster +1) , main="K-Means Clustering Results with K=2", xlab ="", ylab="", pch =20, cex =2) plot(x, col =(km.out$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 3 1 1 3 3 1 1 1 1 3 1 1 3 3 1 1 3 1 3 1 3 3 1 [49] 3 3  [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 3 1 1 3 3 1 1 1 1 3 1 1 3 3 1 1 3 1 3 1 3 3 1 [49] 3 3