gpt4 book ai didi

python - 如何使用差距统计找到层次聚类中的最佳聚类数?

转载 作者:行者123 更新时间:2023-11-30 22:45:45 25 4
gpt4 key购买 nike

我想运行具有单一链接的层次聚类,以对具有 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没有明确定义。如何为 clusGap() 函数指定单链接层次聚类?

Python 中是否有与 clusGap() 等效的函数?

谢谢

最佳答案

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)

关于python - 如何使用差距统计找到层次聚类中的最佳聚类数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41119406/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com