# 聚类分析-R语言

#### 1、随机生成三个簇点：

> c1<-cbind(rnorm(30,2,1),rnorm(30,2,1))

> c2<-cbind(rnorm(30,3,1),rnorm(30,20,1))

> c3<-cbind(rnorm(30,15,1),rnorm(30,25,1))

> v=rbind(c1,c2,c3)

> plot(v)

#### 2、K聚类

> clara(v,3)
Call:  clara(x = v, k = 3)
Medoids:
          [,1]      [,2]
[1,]  2.067384  1.761579
[2,]  3.037691 20.208036
[3,] 15.310366 25.211417
Objective function:  1.236222
Clustering vector:  int [1:90] 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 ...
Cluster sizes:             30 30 30
Best sample:
 [1]  2  4  5  6  7 11 12 13 23 24 25 26 27 29 32 34 37 41 42 43 44 45 47 49 51 52 53 54 57
[30] 59 60 61 62 63 64 65 67 74 75 77 81 82 83 84 85 89

Available components:
 [1] "sample"     "medoids"    "i.med"      "clustering" "objective"  "clusinfo"
 [7] "diss"       "call"       "silinfo"    "data"

> cls3<-clara(v,3)
> clusplot(cls3)

K-means一个缺陷就是需要人为指定聚类数目K，如果k值指定的不好，聚类的效果也不是很好。

#### 3、层次聚类

##### 3.1凝聚层次聚类（agglomerative hierarchical clustering）

agnSingle<-agnes(daisy(v),diss=TRUE,method="single")
> agnComplete<-agnes(daisy(v),diss=TRUE,method="complete")
> agnAverage<-agnes(daisy(v),diss=TRUE,method="average")
> plot(agnSingle)
Hit <Return> to see next plot:
Hit <Return> to see next plot:
> plot(agnComplete)
Hit <Return> to see next plot:
Hit <Return> to see next plot:
> plot(agnAverage)
Hit <Return> to see next plot:
Hit <Return> to see next plot:

##### 3.2分裂层次聚类（divisive hierarchical clustering）

> dv<-diana(v)
> plot(dv)

posted on 2015-08-03 12:24  agiser  阅读(15261)  评论(0编辑  收藏  举报