EM 聚类代替 Kmeans
EM clustering instead of Kmeans
我有以下脚本,可用于使用 kmeans
找到集群的最佳数量。如何使用 EM
集群技术而不是 kmeans
.
更改以下脚本
可重现的例子:
ourdata<- scale(USArrests)
欣赏!
wss <- (nrow(ourdata)-1)*sum(apply(ourdata,2,var))
for (i in 2:10) wss[i] <- sum(kmeans(ourdata,
centers=i)$withinss)
plot(1:10, wss, type="b", xlab="Number of Clusters", ylab="Within groups sum of squares")
EMCluster
包为 运行 基于 EM 模型的聚类提供了多种功能。寻找具有 k = 3 个簇的解决方案的示例:
根据 OP 的评论更新:
您可以使用 fpc::cluster.stats()
计算内平方和以及其他感兴趣的指标。这些可以提取和绘制类似于您的原始 post。提醒一下,您所描述的 "the elbow technique" 是一个不准确的描述,因为肘部技术是一种通用技术,可以并且可以与任何选择的指标一起使用。它不仅用于原始 post.
中的平方和内
library(EMCluster)
library(fpc)
ourdata<- scale(USArrests)
dist_fit <- dist(ourdata)
num_clusters <- 2:4
set.seed(1)
wss <- vapply(num_clusters, function(i_k) {
em_fit <- em.EM(ourdata, nclass = i_k, lab = NULL, EMC = .EMC,
stable.solution = TRUE, min.n = NULL, min.n.iter = 10)
cluster_stats_fit <- fpc::cluster.stats(dist_fit, em_fit$class)
cluster_stats_fit$within.cluster.ss
}, numeric(1))
plot(num_clusters, wss, type="b", xlab="Number of Clusters", ylab="Within groups sum of squares")
我有以下脚本,可用于使用 kmeans
找到集群的最佳数量。如何使用 EM
集群技术而不是 kmeans
.
可重现的例子:
ourdata<- scale(USArrests)
欣赏!
wss <- (nrow(ourdata)-1)*sum(apply(ourdata,2,var))
for (i in 2:10) wss[i] <- sum(kmeans(ourdata,
centers=i)$withinss)
plot(1:10, wss, type="b", xlab="Number of Clusters", ylab="Within groups sum of squares")
EMCluster
包为 运行 基于 EM 模型的聚类提供了多种功能。寻找具有 k = 3 个簇的解决方案的示例:
根据 OP 的评论更新:
您可以使用 fpc::cluster.stats()
计算内平方和以及其他感兴趣的指标。这些可以提取和绘制类似于您的原始 post。提醒一下,您所描述的 "the elbow technique" 是一个不准确的描述,因为肘部技术是一种通用技术,可以并且可以与任何选择的指标一起使用。它不仅用于原始 post.
library(EMCluster)
library(fpc)
ourdata<- scale(USArrests)
dist_fit <- dist(ourdata)
num_clusters <- 2:4
set.seed(1)
wss <- vapply(num_clusters, function(i_k) {
em_fit <- em.EM(ourdata, nclass = i_k, lab = NULL, EMC = .EMC,
stable.solution = TRUE, min.n = NULL, min.n.iter = 10)
cluster_stats_fit <- fpc::cluster.stats(dist_fit, em_fit$class)
cluster_stats_fit$within.cluster.ss
}, numeric(1))
plot(num_clusters, wss, type="b", xlab="Number of Clusters", ylab="Within groups sum of squares")