【问题标题】:Compare clusters with ground truth将集群与基本事实进行比较
【发布时间】:2020-09-16 06:18:41
【问题描述】:

在将我的 HAC 聚类结果与手动标记文档的真实情况进行比较时,我遇到了一个问题。

整个数据集由 9k 个文档组成,而 ground-truth 为 100 个;最后一个允许多标签,而聚类过程仅将文档分配给一个聚类。 它们都被加载到 pandas 数据框中。

集群

id      label
 0          1
 1          4
 2          9
     ....
9k          3

基本事实

id    label1    label2    label3    ...    labeln
 0         0         1         0                1
 1         1         0         1    ...         0
           .....
100        0         0         0    ......      0

列中的“1”表示具有该 ID 的文档已分配给该(或那些)标签(或多个标签)。

集群的数量等于标签的数量(由用户分配),比如说 --> 14

现在我正在这样做:

    # computes all possible pairs
    def all_pairs(partition):
        return list(itertools.combinations(partition, 2))

    # main

    indexes = list(map(int, ground_truth['id'].values.tolist()))
    # reduce clusters_file matching only manually analyzed documents:
    reduced_df = clusters.loc[clusters['id'].isin(indexes), :]

    clusters_groups = reduced_df.groupby('label')
    clusters_k = len(clusters_groups)

    for label, df_group in clusters_groups:

        docs_in_cluster = df_group['id'].values.tolist()
        pairs_docs_in_cluster = all_pairs(docs_in_cluster)

        intersection_list = []

        for col in self.ground_truth.columns[1:]:

            # get pairs for this columns/label
            constraints = list(
                map(int, ground_truth.loc[ground_truth[col] == 1, 'id'].values.tolist())
            )
            pairs_constraints = all_pairs(constraints)

            # find sets intersection (of pairs) between current cluster and ground_truth
            intersection = list(set(pairs_constraints) & set(pairs_docs_in_cluster))

            if len(intersection) > 0:
                # concatenate with other labels from ground_truth
                intersection_list += intersection

        ratio = len(intersection_list) / len(pairs_docs_in_cluster) * 100
        print("Cluster: ", label, "[{}] elements".format(df_group.shape[0]),
              'matched {} unique pairs'.format(len(intersection_list)), "--> {:.2f} %".format(ratio))

我得到了类似的东西:

Automatic clustering: 8469 elements and k=14 clusters
Ground Truth: 107 elements and m=14 labels
    Cluster:  0 [29] elements matched 111 unique pairs --> 27.34 %
    Cluster:  1 [5] elements matched 1 unique pairs --> 10.00 %
    Cluster:  2 [1] elements matched 0 unique pairs --> 0.00 %
    Cluster:  3 [1] elements matched 0 unique pairs --> 0.00 %
    Cluster:  4 [9] elements matched 5 unique pairs --> 13.89 %
    Cluster:  5 [6] elements matched 2 unique pairs --> 13.33 %
    Cluster:  6 [13] elements matched 27 unique pairs --> 34.62 %
    Cluster:  7 [2] elements matched 0 unique pairs --> 0.00 %
    Cluster:  8 [4] elements matched 3 unique pairs --> 50.00 %
    Cluster:  9 [3] elements matched 0 unique pairs --> 0.00 %
    Cluster:  10 [2] elements matched 0 unique pairs --> 0.00 %
    Cluster:  11 [8] elements matched 2 unique pairs --> 7.14 %
    Cluster:  12 [6] elements matched 10 unique pairs --> 66.67 %
    Cluster:  13 [17] elements matched 29 unique pairs --> 21.32 %

这很糟糕,但是如果我检查了聚类结果,它们看起来并没有那么糟糕。 因此,我认为我的评估指标(比率计算)是错误的,但我真的看不出我的错误在哪里(如果有的话)。

【问题讨论】:

  • 您的基本事实有 n 列。这是因为 n 个人手动标记了他们吗?

标签: python cluster-analysis hierarchical-clustering


【解决方案1】:

我建议使用NMI-AMI-score

重新调整 AMI 或(调整互信息)分数,使随机聚类的分数为 0。NMI(标准化互信息)用于集群数量不同的情况,因此通常是一个黄金标准集群社区。

这两个度量的范围都在 0 和 1 之间,其中 0 被认为是随机聚类,而 1 与基本事实完美匹配。

还有其他度量,例如 F-measure 或 Purity。

我不确定,为什么您的“基本事实”中存在许多不同的标签,但也许您可以对这些标签进行多数投票以观察一个基本事实。

【讨论】:

  • 感谢您的回复。我的教授标记了我的基本事实,他在假设文档可以属于多个主题的情况下,使用有关该领域的知识(实时系统文档)来说明每个文档的主题。您如何看待使用混淆矩阵进行分析?
  • 如果您想到混淆矩阵,那么我建议您使用 F-measure(或 F1-score),因为这是准确率和召回率之间的关系。
  • 但是由于我的 GT 仅由 100 个文档组成,我是否必须只考虑数据集中的那些文档?
  • 好的,我不清楚。您可以使用对其他事实进行聚类的相同方法对基本事实进行聚类。然后您可以根据此测试集评估您的方法的聚类效果。不过,您需要确定哪一个 n 列是正确的。
  • 我试着解释一下:我有一个包含 9k 个文档的语料库,我已经对其进行了聚类会话,比如说 Kmeans。之后,我手动阅读了 100 个文档,我说:“好吧,这篇论文讲的是‘实时调度’,另一篇讲的是‘数据库’和‘抢占式调度’,等等……好吧之后,我运行上面的所有代码来检查我所说的有多少文档必须放在一起,实际上是在某个集群中放在一起。
猜你喜欢
  • 2018-04-21
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-06-22
  • 1970-01-01
  • 2012-07-25
相关资源
最近更新 更多