gpt4 book ai didi

r - 从 R 中的切割树状图中提取标签成员/分类(即 : a cutree function for dendrogram)

转载 作者:行者123 更新时间:2023-12-03 23:29:51 27 4
gpt4 key购买 nike

我正在尝试从 R 中的树状图中提取分类,我是 cut在一定的高度。使用 cutree 很容易做到这一点在 hclust 上对象,但我不知道如何在 dendrogram 上执行此操作目的。

此外,我不能只使用来自原始 hclust 的集群,因为(令人沮丧的是)来自 cutree 的类的编号。与 cut 的类编号不同.

hc <- hclust(dist(USArrests), "ave")

classification<-cutree(hc,h=70)

dend1 <- as.dendrogram(hc)
dend2 <- cut(dend1, h = 70)


str(dend2$lower[[1]]) #group 1 here is not the same as
classification[classification==1] #group 1 here

有没有办法让分类相互映射,或者从 dendrogram 中提取较低的分支成员资格对象(也许巧妙地使用了 dendrapply ?)的格式更像是 cutree给?

最佳答案

我建议您使用 cutree来自 dendextend 的函数包裹。它包括一个树状图方法(即: dendextend:::cutree.dendrogram )。

您可以从 its introductory vignette 了解有关该软件包的更多信息.

我应该补充一点,虽然你的函数( classify )很好,但使用 cutree 有几个优点。来自 denextend:

  • 它还允许您使用特定的 k (簇数),而不仅仅是 h (特定的高度)。
  • 这与您从 hclust 上的 cutree 得到的结果一致(classify 不会)。
  • 它通常会更快。

  • 以下是使用代码的示例:
    # Toy data:
    hc <- hclust(dist(USArrests), "ave")
    dend1 <- as.dendrogram(hc)

    # Get the package:
    install.packages("dendextend")
    library(dendextend)

    # Get the package:
    cutree(dend1,h=70) # it now works on a dendrogram
    # It is like using:
    dendextend:::cutree.dendrogram(dend1,h=70)

    顺便说一下,在这个功能的基础上,dendextend 允许用户做更多很酷的事情,比如基于切割树状图的颜色分支/标签:
    dend1 <- color_branches(dend1, k = 4)
    dend1 <- color_labels(dend1, k = 5)
    plot(dend1)

    enter image description here

    最后,这里还有一些代码来演示我的其他观点:
    # This would also work with k:
    cutree(dend1,k=4)

    # and would give identical result as cutree on hclust:
    identical(cutree(hc,h=70) , cutree(dend1,h=70) )
    # TRUE

    # But this is not the case for classify:
    identical(classify(dend1,70) , cutree(dend1,h=70) )
    # FALSE


    install.packages("microbenchmark")
    require(microbenchmark)
    microbenchmark(classify = classify(dend1,70),
    cutree = cutree(dend1,h=70) )
    # Unit: milliseconds
    # expr min lq median uq max neval
    # classify 9.70135 9.94604 10.25400 10.87552 80.82032 100
    # cutree 37.24264 37.97642 39.23095 43.21233 141.13880 100
    # 4 times faster for this tree (it will be more for larger trees)

    # Although (if to be exact about it) if I force cutree.dendrogram to not go through hclust (which can happen for "weird" trees), the speed will remain similar:
    microbenchmark(classify = classify(dend1,70),
    cutree = cutree(dend1,h=70, try_cutree_hclust = FALSE) )
    # Unit: milliseconds
    # expr min lq median uq max neval
    # classify 9.683433 9.819776 9.972077 10.48497 29.73285 100
    # cutree 10.275839 10.419181 10.540126 10.66863 16.54034 100

    如果您正在考虑改进此功能的方法,请在此处进行修补:

    https://github.com/talgalili/dendextend/blob/master/R/cutree.dendrogram.R

    我希望你或其他人会发现这个答案很有帮助。

    关于r - 从 R 中的切割树状图中提取标签成员/分类(即 : a cutree function for dendrogram),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25452472/

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