【问题标题】:How to get non-singleton cluster ids in scipy hierachical clustering如何在 scipy 层次聚类中获取非单例集群 ID
【发布时间】:2014-07-21 04:35:03
【问题描述】:

根据this,我们可以获得非单例集群的标签。

我用一个简单的例子试了一下。

import numpy as np
import scipy.cluster.hierarchy
import matplotlib.pyplot as plt
from scipy.cluster.hierarchy import dendrogram, linkage

mat = np.array([[ 0. , 1. , 3.  ,0. ,2.  ,3.  ,1.],
 [ 1. , 0. , 3. , 1.,  1. , 2. , 2.],
 [ 3.,  3. , 0.,  3. , 3.,  3. , 4.],
 [ 0. , 1. , 3.,  0. , 2. , 3.,  1.],
 [ 2. , 1.,  3. , 2.,  0. , 1.,  3.],
 [ 3. , 2.,  3. , 3. , 1. , 0. , 3.],
 [ 1. , 2.,  4. , 1. , 3.,  3. , 0.]])

def llf(id):
    if id < n:
        return str(id)
    else:
        return '[%d %d %1.2f]' % (id, count, R[n-id,3])


linkage_matrix = linkage(mat, "complete")

dendrogram(linkage_matrix,
           p=4,
           leaf_label_func=llf,
           color_threshold=1,
           truncate_mode='lastp',
           distance_sort='ascending')

plt.show()

n 是什么,在这里计数?在如下图中,我需要知道谁在 (3) 和 (2) 下列出?

【问题讨论】:

    标签: python scipy cluster-analysis hierarchical-clustering


    【解决方案1】:

    我认为该部分的文档不是很清楚,其中的示例代码甚至无法操作。但很明显,1 表示第 2 个观测值,(3) 表示该节点有 3 个观测值。

    如果您想知道这 3 个 obs 是什么。在第二个节点,如果这是你的问题:

    In [51]:
    D4=dendrogram(linkage_matrix,
                  color_threshold=1,
                  p=4,
                  truncate_mode='lastp',
                  distance_sort='ascending')
    D7=dendrogram(linkage_matrix,
                  color_list=['g',]*7,
                  p=7,
                  truncate_mode='lastp',
                  distance_sort='ascending', no_plot=True)  
    from itertools import groupby
    [list(group) for key, group in groupby(D7['ivl'],lambda x: x in D4['ivl'])]
    Out[51]:
    [['1'], ['6', '0', '3'], ['2'], ['4', '5']]
    

    第二个节点包含 obs。第 7 个、第 1 个和第 4 个,第 2 个节点包含第 5 个和第 6 个观测值。

    【讨论】:

      猜你喜欢
      • 2012-03-10
      • 2016-02-17
      • 2021-07-26
      • 2013-07-11
      • 1970-01-01
      • 1970-01-01
      • 2021-07-02
      • 2013-11-22
      相关资源
      最近更新 更多