Team 2 K-means in R (Jang)

1854 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 (4) km.out3 =kmeans (x,3, nstart =20) km.out3 plot(x, col =(km.out3$cluster +1) , main="K-Means Clustering Results with K=3", xlab ="", ylab="", pch =20, cex =2) 
       
K-means clustering with 3 clusters of sizes 13, 25, 12

Cluster means:
        [,1]        [,2]
1  0.9348942  0.22064634
2  2.7112299 -3.95090815
3 -0.6775392  0.01699628

Clustering vector:
 [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

Within cluster sum of squares by cluster:
[1]  9.878084 32.323548 10.712040
 (between_SS / total_SS =  85.2 %)

Available components:

[1] "cluster"      "centers"      "totss"        "withinss"    
"tot.withinss" "betweenss"   
[7] "size"        
K-means clustering with 3 clusters of sizes 13, 25, 12

Cluster means:
        [,1]        [,2]
1  0.9348942  0.22064634
2  2.7112299 -3.95090815
3 -0.6775392  0.01699628

Clustering vector:
 [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

Within cluster sum of squares by cluster:
[1]  9.878084 32.323548 10.712040
 (between_SS / total_SS =  85.2 %)

Available components:

[1] "cluster"      "centers"      "totss"        "withinss"     "tot.withinss" "betweenss"   
[7] "size"        
 
       
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