溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

R語言做K均值聚類的示例分析

發(fā)布時間:2021-11-22 14:17:11 來源:億速云 閱讀:949 作者:柒染 欄目:大數(shù)據(jù)

R語言做K均值聚類的示例分析,相信很多沒有經(jīng)驗的人對此束手無策,為此本文總結(jié)了問題出現(xiàn)的原因和解決方法,通過這篇文章希望你能解決這個問題。

k均值聚類是一種比較常用的聚類方法,R語言里做k均值聚類比較常用的函數(shù)是kmeans(),需要輸入3個參數(shù),第一個是聚類用到的數(shù)據(jù),第二個是你想將數(shù)據(jù)聚成幾類k,第三個參數(shù)是nstarthttps://www.datanovia.com/en/lessons/k-means-clustering-in-r-algorith-and-practical-examples/

這篇鏈接里提到R語言做K均值聚類的示例分析

默認的nstart是1,推薦使用較大的值,以獲得一個穩(wěn)定的結(jié)果。比如可以使用25或者50。

那如果想使用k均值聚類的話,就可以分成兩種情況,

  • 第一種是知道我自己想聚成幾類,比如鳶尾花的數(shù)據(jù)集,明確想聚為3類。這時候直接指定k 下面用鳶尾花數(shù)據(jù)集做k均值聚類
df<-iris[,1:4]
iris.kmeans<-kmeans(df,centers=3,nstart = 25)
names(iris.kmeans)
 

iris.kmeans結(jié)果里存儲9個結(jié)果,可能會用到的是iris.kmeans$cluster存儲的是每個樣本被歸為哪一類iris.kmeans$size存儲的是每一個大類有多少個樣本

使用散點圖展示結(jié)果,借助factoextra包中的fviz_cluster()函數(shù)

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值設(shè)置為一定的范圍,然后根據(jù)聚類結(jié)果里的一些參數(shù)來篩選最優(yōu)的結(jié)果 比如這篇文章 https://www.guru99.com/r-k-means-clustering.html 他提到可以使用      cluster$tot.withinss這個參數(shù),選擇出現(xiàn)平滑變化的那個點,他起的名字是      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這個數(shù)據(jù)集是美國50個州1973年每10萬人中因某種罪被捕的人數(shù),共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這個包里有一個函數(shù)fviz_nbclust()直接可以選擇最優(yōu)的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

從圖上看有部分重疊的地方,還有一種辦法就是把數(shù)據(jù)標(biāo)準(zhǔn)化一下

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均值聚類的示例分析

看完上述內(nèi)容,你們掌握R語言做K均值聚類的示例分析的方法了嗎?如果還想學(xué)到更多技能或想了解更多相關(guān)內(nèi)容,歡迎關(guān)注億速云行業(yè)資訊頻道,感謝各位的閱讀!

向AI問一下細節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI