数据科学与R语言R. python新手日记机器学习

无监督学习 聚类分析②

2018-01-12  本文已影响31人  柳叶刀与小鼠标

划分聚类分析

K 均值聚类

最常见的划分方法是K均值聚类分析。从概念上讲,K均值算法如下:

K均值聚类能处理比层次聚类更大的数据集。由于K均值聚类在开始要随机选择k个中心点,在每次调用函数时可能获得不同的方案。使用
set.seed() 函数可以保证结果是可复制的。此外,聚类方法对初始中心值的选择也很敏感。
kmeans() 函数有一个 nstart 选项尝试多种初始配置并输出最好的一个。

同样是聚类分析,上一次介绍的是层次聚类分法,这种方法输出的聚类树状图是其最大的优点,但是层次分析法的缺点就在于适合的样本数比较小,大概在150个左右。所以,当我们面临更大的数据时,划分聚类法就是更好的选择,虽然没有树状聚类图,却而代之的是圈型的聚类图。

setwd("E:\\Rwork")
library(rattle)
wine <- read.csv("wine.csv")
head(wine)
df <- wine[,-1]#或者wine$Type <- NULL
head(df)
df.scaled <- scale(df)
library(NbClust)
set.seed(1234)
devAskNewPage(ask = TRUE)#按回车输出图形
nc <- NbClust(df.scaled,min.nc = 2,max.nc = 15,method="kmeans")
table(nc$Best.n[1,])
******************************************************************* 
* Among all indices:                                                
* 4 proposed 2 as the best number of clusters 
* 15 proposed 3 as the best number of clusters 
* 1 proposed 10 as the best number of clusters 
* 1 proposed 12 as the best number of clusters 
* 1 proposed 14 as the best number of clusters 
* 1 proposed 15 as the best number of clusters 

                   ***** Conclusion *****                            
 
* According to the majority rule, the best number of clusters is  3 
 
 
******************************************************************* 

barplot(table(nc$Best.n[1,]),
        xlab="Number of Clusters", ylab="Number of Criteria",
        main="Number of Clusters Chosen by 26 Criteria")


set.seed(1234)
fit.km<- kmeans(df.scaled,3,nstart = 25)#nstart=25默认推荐值
fit.km$size #查看具体分类数量  
fit.km$centers#查看具体中心点
aggregate(df,by=list(culster=fit.km$cluster),mean)
ct.km<-table(wine$Type, fit.km$cluster)  #查看分类概括  
ct.km
   
     1  2  3
  1 59  0  0
  2  3 65  3
  3  0  0 48
library(flexclust)
randIndex(ct.km)#-1是完全不同意,1是完全同意
     ARI 
0.897495 

library(cluster)
par(mfrow=c(1,1),no.readonly = FALSE)#NbCluster将图形改为(1,2)的形式,这里将其改回(1,1)的形式
clusplot(df.scaled,fit.km$cluster,color = TRUE,shade = T,labels = 2,lines = 1)#输出聚类图

围绕中心点的划分

因为K均值聚类方法是基于均值的,所以它对异常值是敏感的。一个更稳健的方法是围绕中心点的划分(PAM)。与其用质心(变量均值向量)表示类,不如用一个最有代表性的观测值来表示(称为中心点)。K均值聚类一般使用欧几里得距离,而PAM可以使用任意的距离来计算。因此,PAM可以容纳混合数据类型,并且不仅限于连续变量。
PAM算法如下:

参数详解:可以使用 cluster 包中的 pam() 函数使用基于中心点的划分方法。格式是 pam(x, k,metric="euclidean", stand=FALSE) ,这里的 x 表示数据矩阵或数据框, k 表示聚类的个数,metric 表示使用的相似性/相异性的度量,而 stand 是一个逻辑值,表示是否有变量应该在计算该指标之前。

> library(cluster)
> set.seed(1234)
> fit.pam <- pam(wine[-1], k=3, stand=TRUE)
> fit.pam$medoids

> clusplot(fit.pam, main="Bivariate Cluster Plot")

ct.pam <- table(wine$Type, fit.pam$clustering)
> randIndex(ct.pam)
      ARI 
0.6994957 
上一篇下一篇

猜你喜欢

热点阅读