温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

R语言做K均值聚类的示例分析

发布时间:2021-11-22 14:17:11 来源:亿速云 阅读:933 作者:柒染 栏目:大数据

R语言做K均值聚类的示例分析,相信很多没有经验的人对此束手无策,为此本文总结了问题出现的原因和解决方法,通过这篇文章希望你能解决这个问题。

k均值聚类是一种比较常用的聚类方法,R语言里做k均值聚类比较常用的函数是kmeans(),需要输入3个参数,第一个是聚类用到的数据,第二个是你想将数据聚成几类k,第三个参数是nstarthttps://www.datanovia.com/en/lessons/k-means-clustering-in-r-algorith-and-practical-examples/

这篇链接里提到R语言做K均值聚类的示例分析

默认的nstart是1,推荐使用较大的值,以获得一个稳定的结果。比如可以使用25或者50。

那如果想使用k均值聚类的话,就可以分成两种情况,

  • 第一种是知道我自己想聚成几类,比如鸢尾花的数据集,明确想聚为3类。这时候直接指定k 下面用鸢尾花数据集做k均值聚类
df<-iris[,1:4]
iris.kmeans<-kmeans(df,centers=3,nstart = 25)
names(iris.kmeans)
 

iris.kmeans结果里存储9个结果,可能会用到的是iris.kmeans$cluster存储的是每个样本被归为哪一类iris.kmeans$size存储的是每一个大类有多少个样本

使用散点图展示结果,借助factoextra包中的fviz_cluster()函数

library(factoextra)
fviz_cluster(object=iris.kmeans,data=iris[,1:4],
             ellipse.type = "euclid",star.plot=T,repel=T,
             geom = ("point"),palette='jco',main="",
             ggtheme=theme_minimal())+
  theme(axis.title = element_blank())
 

作图代码参考 https://degreesofbelief.roryquinn.com/clustering-analysis-in-r-part-2R语言做K均值聚类的示例分析

  • 第二种情况是我不知道想要聚成几类,这个时候就可以将k值设置为一定的范围,然后根据聚类结果里的一些参数来筛选最优的结果 比如这篇文章 https://www.guru99.com/r-k-means-clustering.html 他提到可以使用      cluster$tot.withinss这个参数,选择出现平滑变化的那个点,他起的名字是      elbow method,英文解释是

This method uses within-group homogeneity or within-group heterogeneity to evaluate the variability. In other words, you are interested in the percentage of the variance explained by each cluster. You can expect the variability to increase with the number of clusters, alternatively, heterogeneity decreases. Our challenge is to find the k that is beyond the diminishing returns. Adding a new cluster does not improve the variability in the data because very few information is left to explain.

这个英文解释我也没有看明白。实际操作的代码是

下面用USArrests这个数据集是美国50个州1973年每10万人中因某种罪被捕的人数,共4个变量

df<-USArrests
kmean_withinss <- function(k) {
  cluster <- kmeans(df, k,nstart = 25)
  return (cluster$tot.withinss)
}
wss<-sapply(2:20, kmean_withinss)
wss
elbow<-data.frame(A=2:20,B=wss)
library(ggplot2)
ggplot(elbow,aes(x=A,y=B))+
  geom_point()+
  geom_line()+
  scale_x_continuous(breaks = seq(1, 20, by = 1))+theme_bw()
 
R语言做K均值聚类的示例分析
image.png

从上图看,7到8好像是变得比较平滑的,那我们先选7看看

usa.kmeans<-kmeans(df,centers=7,nstart = 25)
fviz_cluster(object=usa.kmeans,df,
             ellipse.type = "euclid",star.plot=T,repel=T,
             geom = c("point","text"),palette='jco',main="",
             ggtheme=theme_minimal())+
  theme(axis.title = element_blank())
 
R语言做K均值聚类的示例分析
image.png

从图上看划分成7类有点多了

https://www.datanovia.com/en/lessons/k-means-clustering-in-r-algorith-and-practical-examples/

这个链接里提到factoextra这个包里有一个函数fviz_nbclust()直接可以选择最优的k

fviz_nbclust(df, kmeans, method = "wss")
 
R语言做K均值聚类的示例分析
image.png

从图上看4到5变得平滑了,选择4试一下

usa.kmeans<-kmeans(df,centers=4,nstart = 25)
fviz_cluster(object=usa.kmeans,df,
             ellipse.type = "euclid",star.plot=T,repel=T,
             geom = c("point","text"),palette='jco',main="",
             ggtheme=theme_minimal())+
  theme(axis.title = element_blank())
 
R语言做K均值聚类的示例分析
image.png

从图上看有部分重叠的地方,还有一种办法就是把数据标准化一下

df1<-scale(df)
usa.kmeans<-kmeans(df1,centers=4,nstart = 25)
fviz_cluster(object=usa.kmeans,df,
             ellipse.type = "euclid",star.plot=T,repel=T,
             geom = c("point","text"),palette='jco',main="",
             ggtheme=theme_minimal())+
  theme(axis.title = element_blank())
 
R语言做K均值聚类的示例分析

看完上述内容,你们掌握R语言做K均值聚类的示例分析的方法了吗?如果还想学到更多技能或想了解更多相关内容,欢迎关注亿速云行业资讯频道,感谢各位的阅读!

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI