十年網(wǎng)站開發(fā)經(jīng)驗 + 多家企業(yè)客戶 + 靠譜的建站團隊
量身定制 + 運營維護+專業(yè)推廣+無憂售后,網(wǎng)站問題一站解決
R語言做K均值聚類的示例分析,相信很多沒有經(jīng)驗的人對此束手無策,為此本文總結(jié)了問題出現(xiàn)的原因和解決方法,通過這篇文章希望你能解決這個問題。
十年的紅河哈尼網(wǎng)站建設經(jīng)驗,針對設計、前端、開發(fā)、售后、文案、推廣等六對一服務,響應快,48小時及時工作處理。全網(wǎng)整合營銷推廣的優(yōu)勢是能夠根據(jù)用戶設備顯示端的尺寸不同,自動調(diào)整紅河哈尼建站的顯示方式,使網(wǎng)站能夠適用不同顯示終端,在瀏覽器中調(diào)整網(wǎng)站的寬度,無論在任何一種瀏覽器上瀏覽網(wǎng)站,都能展現(xiàn)優(yōu)雅布局與設計,從而大程度地提升瀏覽體驗。創(chuàng)新互聯(lián)建站從事“紅河哈尼網(wǎng)站設計”,“紅河哈尼網(wǎng)站推廣”以來,每個客戶項目都認真落實執(zhí)行。
k均值聚類是一種比較常用的聚類方法,R語言里做k均值聚類比較常用的函數(shù)是kmeans()
,需要輸入3個參數(shù),第一個是聚類用到的數(shù)據(jù),第二個是你想將數(shù)據(jù)聚成幾類k
,第三個參數(shù)是nstart
https://www.datanovia.com/en/lessons/k-means-clustering-in-r-algorith-and-practical-examples/
這篇鏈接里提到
默認的nstart是1,推薦使用較大的值,以獲得一個穩(wěn)定的結(jié)果。比如可以使用25或者50。
那如果想使用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-2
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()
從上圖看,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())
從圖上看劃分成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")
從圖上看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())
從圖上看有部分重疊的地方,還有一種辦法就是把數(shù)據(jù)標準化一下
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())
看完上述內(nèi)容,你們掌握R語言做K均值聚類的示例分析的方法了嗎?如果還想學到更多技能或想了解更多相關(guān)內(nèi)容,歡迎關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道,感謝各位的閱讀!