如何在层次聚类中利用间隙统计寻找最优聚类数?

2024-04-29 06:29:54 发布

您现在位置:Python中文网/ 问答频道 /正文

我想用一个链接运行层次聚类,用300个特性和1500个观察值对文档进行聚类。我想找到这个问题的最佳簇数。在

下面的链接使用下面的代码来查找具有最大间隙的簇的数量。在

http://www.sthda.com/english/wiki/determining-the-optimal-number-of-clusters-3-must-known-methods-unsupervised-machine-learning

# Compute gap statistic 
set.seed(123)

iris.scaled <- scale(iris[, -5])

gap_stat <- clusGap(iris.scaled, FUN = hcut, K.max = 10, B = 50)

# Plot gap statistic 
fviz_gap_stat(gap_stat)

但在这一环节中,hcut的定义并不明确。{cdi>如何指定单层次链接?在

在python中我们是否有一个等价的clusGap()?在

谢谢


Tags: 代码文档httpiris数量链接聚类特性
1条回答
网友
1楼 · 发布于 2024-04-29 06:29:54

hcut()函数是您发布的链接中使用的factorextra包的一部分:

hcut package:factoextra R Documentation

Computes Hierarchical Clustering and Cut the Tree

Description:

 Computes hierarchical clustering (hclust, agnes, diana) and cut
 the tree into k clusters. It also accepts correlation based
 distance measure methods such as "pearson", "spearman" and
 "kendall".

R还有一个内置函数hclust(),可用于执行层次聚类。但是,在默认情况下,它不执行单链接聚类,因此不能简单地将hcut替换为hclust。在

但是,如果您查看clusGap()的帮助,您将看到您可以提供要应用的自定义群集函数:

FUNcluster: a ‘function’ which accepts as first argument a (data) matrix like ‘x’, second argument, say k, k >= 2, the number of clusters desired, and returns a ‘list’ with a component named (or shortened to) ‘cluster’ which is a vector of length ‘n = nrow(x)’ of integers in ‘1:k’ determining the clustering or grouping of the ‘n’ observations.

hclust()函数能够执行单链接层次聚类,因此您可以执行以下操作:

cluster_fun <- function(x, k) list(cluster=cutree(hclust(dist(x), method="single"), k=k))
gap_stat <- clusGap(iris.scaled, FUN=cluster_fun, K.max=10, B=50)

相关问题 更多 >