0% found this document useful (0 votes)
11 views2 pages

Kmeansrcode

sourcecode for kmeans

Uploaded by

suguna
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
11 views2 pages

Kmeansrcode

sourcecode for kmeans

Uploaded by

suguna
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 2

data(iris)

> View(iris)
> head(iris)
Sepal.Length Sepal.Width Petal.Length Petal.Width
Species
1 5.1 3.5 1.4 0.2
setosa
2 4.9 3.0 1.4 0.2
setosa
3 4.7 3.2 1.3 0.2
setosa
4 4.6 3.1 1.5 0.2
setosa
5 5.0 3.6 1.4 0.2
setosa
6 5.4 3.9 1.7 0.4
setosa
> x = iris[,-5]
> y = iris$Species
> kc <- kmeans(x,3)
> kc
K-means clustering with 3 clusters of sizes 38, 62, 50

Cluster means:
Sepal.Length Sepal.Width Petal.Length Petal.Width
1 6.850000 3.073684 5.742105 2.071053
2 5.901613 2.748387 4.393548 1.433871
3 5.006000 3.428000 1.462000 0.246000

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 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 2 2 1 2 2
2 2
[58] 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 1 2 2 2 2 2
2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 1 2 1 1 1 1 2 1 1 1 1 1
1 2
[115] 2 1 1 1 1 2 1 2 1 2 1 1 2 2 1 1 1 1 1 2 1 1 1 1 2 1
1 1 2 1 1 1 2 1 1 2

Within cluster sum of squares by cluster:


[1] 23.87947 39.82097 15.15100
(between_SS / total_SS = 88.4 %)

Available components:

[1] "cluster" "centers" "totss"


"withinss" "tot.withinss" "betweenss" "size"
[8] "iter" "ifault"
>
table(y,kc$cluster)

y 1 2 3
setosa 0 0 50
versicolor 2 48 0
virginica 36 14 0
> plot(x[c("Sepal.Length", "Sepal.Width")], col=kc$cluster)
> points(kc$centers[,c("Sepal.Length", "Sepal.Width")], col=1:3, pch=23,
cex=3)

You might also like