用python计算准确率_python – 获得scikit-learn中多标签预测的准确性

您可以自己编写一个版本,这是一个不考虑权重和规范化的示例.

import numpy as np

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

y_pred = np.array([[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

https://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,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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值