4

我在这里搜索并用谷歌搜索,但无济于事。在 Weka 中进行聚类时,有一个方便的选项,即类到聚类,它将算法产生的聚类(例如简单的 k-means)与您作为类属性提供的“基本事实”类标签相匹配。这样我们就可以看到集群准确率(不正确的百分比)。

现在,我如何在 Matlab 中实现这一点,即将我的clusterClasses向量 eg[1, 1, 2, 1, 3, 2, 3, 1, 1, 1]转换为与提供的地面实况标签向量 eg 相同的索引[2, 2, 2, 3, 1, 3]

我想可能是基于集群中心和标签中心,但我不知道如何实现!

任何帮助将不胜感激。

文森特

4

4 回答 4

4

几个月前,我在进行聚类时偶然发现了一个类似的问题。我没有很长时间搜索内置解决方案(尽管我确信它们一定存在),最后编写了我自己的小脚本,以将我找到的标签与基本事实相匹配。该代码非常粗糙,但它应该可以帮助您入门。

它基于尝试所有可能的标签重新排列,以查看最适合真值向量的女巫。这意味着给定一个yte = [3 3 2 1]带有 ground truth的聚类结果y = [1 1 2 3],脚本将尝试匹配[3 3 2 1], [3 3 1 2], [2 2 3 1], [2 2 1 3], [1 1 2 3] and [1 1 3 2]y找到最佳匹配。

这是基于使用内置脚本perms()无法处理超过 10 个独特的集群。对于 7-10 个独特的集群,代码也可能会变慢,因为复杂性会随着因子的增长而增长。

function [accuracy, true_labels, CM] = calculateAccuracy(yte, y)
%# Function for calculating clustering accuray and matching found 
%# labels with true labels. Assumes yte and y both are Nx1 vectors with
%# clustering labels. Does not support fuzzy clustering.
%#
%# Algorithm is based on trying out all reorderings of cluster labels, 
%# e.g. if yte = [1 2 2], try [1 2 2] and [2 1 1] so see witch fit 
%# the truth vector the best. Since this approach makes use of perms(),
%# the code will not run for unique(yte) greater than 10, and it will slow
%# down significantly for number of clusters greater than 7.
%#
%# Input:
%#   yte - result from clustering (y-test)
%#   y   - truth vector
%#
%# Output:
%#   accuracy    -   Overall accuracy for entire clustering (OA). For
%#                   overall error, use OE = 1 - OA.
%#   true_labels -   Vector giving the label rearangement witch best 
%#                   match the truth vector (y).
%#   CM          -   Confusion matrix. If unique(yte) = 4, produce a
%#                   4x4 matrix of the number of different errors and  
%#                   correct clusterings done.

N = length(y);

cluster_names = unique(yte);
accuracy = 0;
maxInd = 1;

perm = perms(unique(y));
[pN pM] = size(perm);

true_labels = y;

for i=1:pN
    flipped_labels = zeros(1,N);
    for cl = 1 : pM
        flipped_labels(yte==cluster_names(cl)) = perm(i,cl);
    end

    testAcc = sum(flipped_labels == y')/N;
    if testAcc > accuracy
        accuracy = testAcc;
        maxInd = i;
        true_labels = flipped_labels;
    end

end

CM = zeros(pM,pM);
for rc = 1 : pM
    for cc = 1 : pM
        CM(rc,cc) = sum( ((y'==rc) .* (true_labels==cc)) );
    end
end

例子:

[acc newLabels CM] = calculateAccuracy([3 2 2 1 2 3]',[1 2 2 3 3 3]')

acc =

0.6667


newLabels =

 1     2     2     3     2     1


CM =

 1     0     0
 0     2     0
 1     1     1
于 2012-07-27T08:53:42.603 回答
1

您可能想研究更灵活的评估集群的方法。例如配对计数指标。

“类=集群”假设对于从机器学习进入集群的人来说是典型的。但相反,您应该假设某些类可能由多个集群组成,或者多个类实际上是集群的。这些是聚类算法应该实际检测到的有趣情况。

于 2012-07-27T20:55:13.723 回答
1

我需要 Python 的这个确切的东西,并转换了 Vidar 发布的代码(接受的答案)。我将代码分享给任何感兴趣的人。我重命名了变量并删除了混淆矩阵(大多数机器学习库都为此内置了函数)。我注意到文森特(http://www.mathworks.com/matlabcentral/fileexchange/32197-clustering-results-measurement)链接的更快实现为时已晚。可能更好地适应 Python。

#tested with python 3.6
def remap_labels(pred_labels, true_labels):
    """Rename prediction labels (clustered output) to best match true labels."""
    # from itertools import permutations # import this into script.
    pred_labels, true_labels = np.array(pred_labels), np.array(true_labels)
    assert pred_labels.ndim == 1 == true_labels.ndim
    assert len(pred_labels) == len(true_labels)
    cluster_names = np.unique(pred_labels)
    accuracy = 0

    perms = np.array(list(permutations(np.unique(true_labels))))

    remapped_labels = true_labels
    for perm in perms:
        flipped_labels = np.zeros(len(true_labels))
        for label_index, label in enumerate(cluster_names):
            flipped_labels[pred_labels == label] = perm[label_index]

        testAcc = np.sum(flipped_labels == true_labels) / len(true_labels)
        if testAcc > accuracy:
            accuracy = testAcc
            remapped_labels = flipped_labels

    return accuracy, remapped_labels
于 2020-01-11T01:54:35.593 回答
0

遵循@Vidar 的想法,我编写了自己的代码来查找最佳标签对应关系。我假设初始标签是整数 $0..K$ 并找到最匹配的标签重命名。该代码依赖于功能itertools.permutations(由this answer建议):

import numpy as np
from itertools import permutations

def match_labels(labels, labels0, K):
    perms = list(permutations(range(K)))
    nmatch = 0
    for jperm, perm in enumerate(perms):
        labs = np.array([perm[l] for l in labels])
        newmatch = (labs == labels0).sum()
        if newmatch > nmatch:
            nmatch = newmatch
            newlabels = labs
            matchtable = list(perm)
    return newlabels, matchtable

该线程建议使用更先进的技术来验证聚类的质量。第一个尝试的可能是Rand index,它在许多库中都可用(例如,在scikit-learn 中)。

于 2022-01-14T15:16:28.980 回答