set.seed (4)
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
plot(x, col =(km.out$cluster +1) , main="K-Means Clustering
Results with K=3", xlab ="", ylab="", pch =20, cex =2)
|
K-means clustering with 3 clusters of sizes 14, 11, 25
Cluster means:
[,1] [,2]
1 -0.7532448 -0.39907658
2 0.8968634 0.05541174
3 3.4968274 -3.88442866
Clustering vector:
[1] 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 1 2 2 1 2 2 2 1 1
2 1 1 2 1 1 2 1 2 1 1 1 2 1
[49] 2 1
Within cluster sum of squares by cluster:
[1] 9.796515 14.391060 33.865779
(between_SS / total_SS = 85.5 %)
Available components:
[1] "cluster" "centers" "totss" "withinss"
"tot.withinss" "betweenss"
[7] "size"
K-means clustering with 3 clusters of sizes 14, 11, 25
Cluster means:
[,1] [,2]
1 -0.7532448 -0.39907658
2 0.8968634 0.05541174
3 3.4968274 -3.88442866
Clustering vector:
[1] 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 1 2 2 1 2 2 2 1 1 2 1 1 2 1 1 2 1 2 1 1 1 2 1
[49] 2 1
Within cluster sum of squares by cluster:
[1] 9.796515 14.391060 33.865779
(between_SS / total_SS = 85.5 %)
Available components:
[1] "cluster" "centers" "totss" "withinss" "tot.withinss" "betweenss"
[7] "size"
|