scikit学习中多标签预测精度的获取

2024-05-14 09:57:35 发布

您现在位置:Python中文网/ 问答频道 /正文

multilabel classification设置中,^{}只计算子集精度(3):即,为样本预测的标签集必须与y中的对应标签集完全匹配。

这种计算精度的方法有时被称为精确匹配比(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.”俄勒冈州立大学,Corvallis(2010)。

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

(3)Ghamrawi、Nadia和Andrew McCallum。”Collective multi-label classification.“第14届ACM信息和知识管理国际会议记录”。ACM,2005年。

(4)戈德博勒、尚塔努和苏尼塔·萨拉瓦吉。”Discriminative methods for multi-labeled classification.“知识发现和数据挖掘的进展。斯普林格柏林海德堡,2004年。22-30岁。


Tags: 方法信息for精度scikitmultilabel子集
2条回答

对于2019年的读者,现在可以使用^{}

>>> import numpy as np
>>> hamming_loss(np.array([[0, 1], [1, 1]]), np.zeros((2, 2)))
0.75

在Numpy中也很容易计算:

# Exact Match ratio:
np.all(y_pred == y_true, axis=1).mean()

# Hamming Score:
(y_pred == y_true).mean()

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

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

相关问题 更多 >

    热门问题