pythonnumpy求top-kaccuracy指标
top-k acc表⽰在多分类情况下取最⾼的k类得分的label,与真实值匹配,只要有⼀个label match,结果就是True。如对于⼀个有5类的多分类任务
a_real =1
a_pred =[0.02,0.23,0.35,0.38,0.02]
#top-1
a_pred_label = 3 match = False
#top-3
a_pred_label_list = [1, 2, 3] match = True
对于top-1 accuracy
#5类:0,1,2,3,4
import numpy as nprandom python
a_real = np.array([[1],[2],[1],[3]])
#⽤随机数代替分数
random_score = np.random.rand((4,5))
a_pred_score = random_score / random_score.sum(axis=1).reshape(random_score.shape[0],1)
k = 3 #top-3
#5类:0,1,2,3,4
import numpy as np
a_real = np.array([[1],[2],[1],[3]])
#⽤随机数代替分数
random_score = np.random.rand((4,5))
a_pred_score = random_score / random_score.sum(axis=1).reshape(random_score.shape[0],1)
k =3#top-3
#以下是计算⽅法
max_k_preds = a_pred_score.argsort(axis=1)[:,-k:][:,::-1]#得到top-k label
match_array = np.duce(max_k_preds==a_real, axis=1)#得到匹配结果
topk_acc_score = match_array.sum()/ match_array.shape[0]
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论