python - 在 scikit-learn 中获得多标签预测的准确性

标签 python scikit-learn classification

multilabel classification中设置,sklearn.metrics.accuracy_score仅计算子集精度 (3):即为样本预测的标签集必须与 y_true 中相应的标签集完全匹配。

这种计算准确性的方法有时被命名为,也许不太含糊,精确匹配率 (1):

enter image description here

有没有办法获得另一种典型的方法来计算 scikit-learn 中的准确性,即

enter image description here

(如(1)和(2)中所定义,并且更明确地称为汉明分数(4)(因为它与汉明损失密切相关),或基于标签的 准确率) ?

<小时/>

(1) Sorower, Mohammad S.“A literature survey on algorithms for multi-label learning.”俄勒冈州立大学,科瓦利斯 (2010)。

(2) Tsoumakas、Grigorios 和 Ioannis Katakis。 "Multi-label classification: An overview. "希腊塞萨洛尼基亚里士多德大学信息学系 (2006)。

(3) 加姆拉维、纳迪亚和安德鲁·麦卡勒姆。 》 Collective multi-label classification. 》第14届ACM信息与知识管理国际 session 论文集。美国计算机协会,2005 年。

(4) Godbole、Shantanu 和 Sunita Sarawagi。 ” Discriminative methods for multi-labeled classification. “知识发现和数据挖掘的进展。施普林格柏林海德堡,2004。22-30。

最佳答案

你可以自己写一个版本,这里是一个不考虑权重和标准化的例子。

import numpy as np

y_true = np.array([[0,1,0],
                   [0,1,1],
                   [1,0,1],
                   [0,0,1]])

y_pred = np.array([[0,1,1],
                   [0,1,1],
                   [0,1,0],
                   [0,0,0]])

def hamming_score(y_true, y_pred, normalize=True, sample_weight=None):
    '''
    Compute the Hamming score (a.k.a. label-based accuracy) for the multi-label case
    http://stackoverflow.com/q/32239577/395857
    '''
    acc_list = []
    for i in range(y_true.shape[0]):
        set_true = set( np.where(y_true[i])[0] )
        set_pred = set( np.where(y_pred[i])[0] )
        #print('\nset_true: {0}'.format(set_true))
        #print('set_pred: {0}'.format(set_pred))
        tmp_a = None
        if len(set_true) == 0 and len(set_pred) == 0:
            tmp_a = 1
        else:
            tmp_a = len(set_true.intersection(set_pred))/\
                    float( len(set_true.union(set_pred)) )
        #print('tmp_a: {0}'.format(tmp_a))
        acc_list.append(tmp_a)
    return np.mean(acc_list)

if __name__ == "__main__":
    print('Hamming score: {0}'.format(hamming_score(y_true, y_pred))) # 0.375 (= (0.5+1+0+0)/4)

    # For comparison sake:
    import sklearn.metrics

    # Subset accuracy
    # 0.25 (= 0+1+0+0 / 4) --> 1 if the prediction for one sample fully matches the gold. 0 otherwise.
    print('Subset accuracy: {0}'.format(sklearn.metrics.accuracy_score(y_true, y_pred, normalize=True, sample_weight=None)))

    # Hamming loss (smaller is better)
    # $$ \text{HammingLoss}(x_i, y_i) = \frac{1}{|D|} \sum_{i=1}^{|D|} \frac{xor(x_i, y_i)}{|L|}, $$
    # where
    #  - \\(|D|\\) is the number of samples  
    #  - \\(|L|\\) is the number of labels  
    #  - \\(y_i\\) is the ground truth  
    #  - \\(x_i\\)  is the prediction.  
    # 0.416666666667 (= (1+0+3+1) / (3*4) )
    print('Hamming loss: {0}'.format(sklearn.metrics.hamming_loss(y_true, y_pred))) 

输出:

Hamming score: 0.375
Subset accuracy: 0.25
Hamming loss: 0.416666666667

关于python - 在 scikit-learn 中获得多标签预测的准确性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59195168/

相关文章:

python - 在 matplotlib 中使用 pcolor 具有不同阴影颜色的多个阴影区域

python - 试图导入模块 : undefined symbol: PyUnicodeUCS4_DecodeUTF8

python - 异构特征的featureUnion

R中使用rpart包的ROC曲线?

twitter - 预测 Twitter 上 future 推文的情绪

R预测函数类型="class"错误

python - 使用 Pandas 的 2 天跨度移动平均值

python - 计算 Pandas 数据框列中的元素

machine-learning - 分类与回归?

machine-learning - 支持稀疏矩阵和多标签输出的逻辑回归?