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均值聚類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-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個變量
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()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())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")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())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())image.png
標準化以後的效果看起來好了很多
好了,今天就到這裡了。
歡迎大家關注我的公眾號
小明的數據分析筆記本