前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >R语言做K均值聚类的一个简单小例子

R语言做K均值聚类的一个简单小例子

作者头像
用户7010445
发布2020-09-29 18:34:43
2.1K0
发布2020-09-29 18:34:43
举报
参考链接
  • https://www.guru99.com/r-k-means-clustering.html
  • https://datascienceplus.com/k-means-clustering-in-r/
  • https://www.datanovia.com/en/lessons/k-means-clustering-in-r-algorith-and-practical-examples/

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

这篇链接里提到

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

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

  • 第一种是知道我自己想聚成几类,比如鸢尾花的数据集,明确想聚为3类。这时候直接指定k 下面用鸢尾花数据集做k均值聚类
代码语言:javascript
复制
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()函数

代码语言:javascript
复制
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-2

  • 第二种情况是我不知道想要聚成几类,这个时候就可以将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个变量

代码语言:javascript
复制
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()

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

代码语言:javascript
复制
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())

image.png

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

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

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

代码语言:javascript
复制
fviz_nbclust(df, kmeans, method = "wss")

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

代码语言:javascript
复制
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())

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

代码语言:javascript
复制
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())

标准化以后的效果看起来好了很多

好了,今天就到这里了。

本文参与 腾讯云自媒体分享计划,分享自微信公众号。
原始发表:2020-09-27,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 小明的数据分析笔记本 微信公众号,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 参考链接
相关产品与服务
对象存储
对象存储(Cloud Object Storage,COS)是由腾讯云推出的无目录层次结构、无数据格式限制,可容纳海量数据且支持 HTTP/HTTPS 协议访问的分布式存储服务。腾讯云 COS 的存储桶空间无容量上限,无需分区管理,适用于 CDN 数据分发、数据万象处理或大数据计算与分析的数据湖等多种场景。
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档