1. 首页
  2. 自学中心
  3. 软件
  4. R

R语言学习笔记之k-means Clustering

k-means Clustering
This page demonstrates k-means clustering with R.
> newiris <- iris
> newiris$Species <- NULL
Apply kmeans to newiris, and store the clustering result in kc. The cluster number is set to 3.
> (kc <- kmeans(newiris, 3))
K-means clustering with 3 clusters of sizes 38, 50, 62
Cluster means:
Sepal.Length Sepal.Width Petal.Length Petal.Width
1 6.850000 3.073684 5.742105 2.071053
2 5.006000 3.428000 1.462000 0.246000
3 5.901613 2.748387 4.393548 1.433871
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 2 2 2 2
[30] 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 3 3 1 3 3 3 3 3
[59] 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 1 3 3 3 3 3 3 3 3 3
[88] 3 3 3 3 3 3 3 3 3 3 3 3 3 1 3 1 1 1 1 3 1 1 1 1 1 1 3 3 1
[117] 1 1 1 3 1 3 1 3 1 1 3 3 1 1 1 1 1 3 1 1 1 1 3 1 1 1 3 1 1
[146] 1 3 1 1 3
Within cluster sum of squares by cluster:
[1] 23.87947 15.15100 39.82097
Available components:
[1] “cluster” “centers” “withinss” “size”
Compare the Species label with the clustering result
> table(iris$Species, kc$cluster)
1 2 3
setosa 0 50 0
versicolor 2 0 48
virginica 36 0 14
Plot the clusters and their centres. Note that there are four dimensions in the data and that only the first two dimensions are used to draw the plot below. Some black points close to the green centre (asterisk) are actually closer to the black centre in the four dimensional space.
> plot(newiris[c(“Sepal.Length”, “Sepal.Width”)], col=kc$cluster)
> points(kc$centers[,c(“Sepal.Length”, “Sepal.Width”)], col=1:3, pch=8, cex=2)

152458xz8rcflgbf7a37ab

原创文章,作者:xsmile,如若转载,请注明出处:http://www.17bigdata.com/r%e8%af%ad%e8%a8%80%e5%ad%a6%e4%b9%a0%e7%ac%94%e8%ae%b0%e4%b9%8bk-means-clustering/

发表评论

登录后才能评论

评论列表(1条)

  • 一直很安静 2017年2月27日 下午4:31

    > plot(newiris[c(Sepal.Length, Sepal.Width)], col=kc$cluster)
    > points(kc$centers[,c(Sepal.Length, Sepal.Width)], col=1:3, pch=8, cex=2)

联系我们

在线咨询:点击这里给我发消息

邮件:23683716@qq.com

跳至工具栏