当前位置:网站首页>评估指标及代码实现(NDCG)
评估指标及代码实现(NDCG)
2022-06-22 19:27:00 【Weiyaner】
针对排序常用的评估指标,给出其计算原理及代码实现
排序评估指标
NDCG
1 原理
NDCG全称为 Normalized Discounted Cumulative Gain(归一化折损累计增益),通常用在搜索排序任务中,在这样的任务里,通常会返回一个list作为搜索排序的结果进行输出,为了验证这个list的合理性,就需要对这个list的排序进行评价。这也是NDCG的由来。
Gain: G,增益。
在排序list中,增益指的就是里面的相关性得分,也就是模型的预测结果。rel(i)表示item(i)相关性得分。
Culumatative Gain:CG,累计增益。
对k个rel(i)进行叠加,不考虑位置关系。
C G k = ∑ i = 1 k r e l ( i ) CG_k=\sum_{i=1}^krel(i) CGk=i=1∑krel(i)Discounted Cumulative Gain: DCG,折损累计增益。
考虑排序顺序的因素,使得排名靠前的item增益更高,对排名靠后的item进行折损。DCG认为排在前面的贡献度更大,后面的贡献度较小,也就是对增益值进行加权求和,权重就是位置引起的。
D C G k = ∑ i = 1 k r e l ( i ) l o g 2 ( i + 1 ) DCG_k=\sum_{i=1}^k\frac{rel(i)}{log_2(i+1)} DCGk=i=1∑klog2(i+1)rel(i)
或者:
D C G k = ∑ i = 1 k 2 r e l ( i ) + 1 l o g 2 ( i + 1 ) DCG_k=\sum_{i=1}^k\frac{2^{rel(i)}+1}{log_2(i+1)} DCGk=i=1∑klog2(i+1)2rel(i)+1
也即是说:i越大,排序越往后,对应的 l o g ( i + 1 ) log(i+1) log(i+1)就越大,折损就越高。iDCG,最好排列的的DCG
根据rel(i)进行降序排列,以此序列计算DCG,也就是最好的DCG,称为iDCG。在计算中,采用labels的相关性得分计算(隐形就是0,1;显性评分则是1-5分数)。
如果是隐性评分,根据NDCG,归一化折损累计增益
由于不同搜索的结果返回长度不一样,这样的iDCG就是一个绝对值,没法比较,因此通过DCG/iDCG来表示NDCG,代表着一个相对程度。
N D C G = D C G i D C G NDCG = \frac{DCG}{iDCG} NDCG=iDCGDCG
2 代码实现
上面的理论乍一看理解起来很简单,但是真到具体应用的时候,发现还是很复杂的,以后很多问题需要思考,比如,里面的相似性得分,排序根据什么得分排序等等。代码的实现也容易绕晕。下面给出两种代码方式,分别是只能计算隐性得分的torch版本和numpy版本
torch
# socres为对应item(i)的预测得分,labels对item(i)的标签,由于是隐形评分数据,只有0,1点击值
scores = torch.tensor([[0,0.1,0.3,0.4,0.5]])
labels = torch.tensor([[0,1,1,0,1]])
k = 5
# 降序排列,获取推荐列表的id
rank = (-scores).argsort(dim=1)
cut = rank[:, :k]
# 获取相关性得分,也就是0,1,如果命中
hits = labels.gather(1, cut)
# 计算位置关系,从2开始计
position = torch.arange(2, 2+k)
# 根据位置关系计算位置权重
weights = 1 / torch.log2(position+1)
# 计算DCG
dcg = (hits* weights).sum(1)
# 计算iDCG,由于相关性得分为0,1,且经过排序,所以计算前面为1对应weights之和即可。
idcg = torch.Tensor([weights[:min(n, k)].sum() for n in labels.sum(1)])
ndcg = dcg / idcg
print(ndcg)
numpy
def getDCG(scores):
return np.sum(
np.divide(np.power(2, scores) - 1, np.log2(np.arange(scores.shape[0], dtype=np.float32) + 2)+1),
# np.divide(scores, np.log2(np.arange(scores.shape[0], dtype=np.float32) + 2)+1),
dtype=np.float32)
def getNDCG(rank_list, pos_items):
relevance = np.ones_like(pos_items)
it2rel = {
it: r for it, r in zip(pos_items, relevance)}
rank_scores = np.asarray([it2rel.get(it, 0.0) for it in rank_list], dtype=np.float32)
print(rank_scores)
idcg = getDCG(relevance)
dcg = getDCG(rank_scores)
if dcg == 0.0:
return 0.0
ndcg = dcg / idcg
return ndcg
## l1是推荐排序列表,l2是真实点击的列表
l1 = [4,3,2,1,0]
l2 = [4,2,1]
a = getNDCG(l1, l2)
print(a)
边栏推荐
- 什么?你居然不会微信分身
- Implementation of UART with analog serial port
- UnityEditor 编辑器脚本执行菜单
- 理财产品在双休日可以赎回吗?
- Teach you how to create SSM project structure in idea
- Software testing - Test Case Design & detailed explanation of test classification
- Summary of 2019: 31 is just another start
- The road to systematic construction of geek planet business monitoring and alarm system
- Security policy and NAT (easy IP) of firewall Foundation
- Resolved: can there be multiple auto incrementing columns in a table
猜你喜欢

Simple integration of client go gin 11 delete
Oracle system/用户被锁定的解决方法

AAAI 2022 | traditional Gan can be interpreted after modification, and the interpretability of convolution kernel and the authenticity of generated image are guaranteed

阿里云视频点播播放出错,控制台访问出现code:4400

MySQL foundation - constraints

Alibaba cloud video on demand playback error, console access code:4400

Introduction of Neural Network (BP) in Intelligent Computing

How to realize @ person function in IM instant messaging
Summary of 2019: 31 is just another start

One picture decoding opencloudos community open day
随机推荐
[proteus simulation] 8x8LED dot matrix digital cyclic display
86-给参加<SQL写法与改写培训>的学员补充一个二手案例
扩展Ribbon支持基于元数据的版本管理
Using qtest for data set test performance test GUI test
CVPR 2022 Oral | 视频文本预训练新SOTA,港大、腾讯ARC Lab推出基于多项选择题的借口任务
他98年的,我玩不过他...
Cloud computing in the metauniverse to enhance your digital experience
智能計算之神經網絡(BP)介紹
Teach you how to create SSM project structure in idea
用RNN & CNN进行情感分析 - PyTorch
A detailed solution to mysql8.0 forgetting password
已解决:一個錶中可以有多個自增列嗎
慕课5、服务发现-Nacos
【已解决】--go_out: protoc-gen-go: Plugin failed with status code 1.
6月第3周B站榜单丨飞瓜数据UP主成长排行榜(哔哩哔哩平台)发布!
Introduction of neural network (BP) in Intelligent Computing
深度学习常用损失函数总览:基本形式、原理、特点
R language universalbank CSV "data analysis
极客星球 | 业务监控及告警系统体系化建设之路
AAAI 2022 | 传统GAN修改后可解释,并保证卷积核可解释性和生成图像真实性