用scikit-learn计算NDCG

简介

NDCG是衡量Ranking quality是重要指标。本文将用实际Python例子演示怎样计算NDCG。

用scikit计算NDCG例子

注意ndcg_score接收的参数都是list of list。后面解释为什么是list of list。

如果评估一个排序请求,用以下例子

from sklearn.metrics import ndcg_score

y_truth = [0, 1, 2, 3]
y_predict = [0, 1, 2, 3]

ndcg = ndcg_score([y_truth], [y_predict])
print(f'ndcg = {ndcg}')

如果有多个排序请求,比如日志里记录的一天收到的所有排序请求,用以下例子(假设有3个排序请求)

from sklearn.metrics import ndcg_score

y_truth1 = [0, 1, 2, 3]
y_predict2 = [0, 1, 2, 3]

y_truth2 = [0, 1, 2, 3]
y_predict2 = [0, 1, 2, 3]

y_truth3 = [0, 1, 2, 3]
y_predict3 = [0, 1, 2, 3]

ndcg = ndcg_score(
    [y_truth1, y_truth2, y_truth3], 
    [y_predict1, y_predict2, y_predict3]
)
print(f'ndcg = {ndcg}')

3个排序会产生3个NDCG,最后结果为所有NDCG的平均值,作为这么多请求的总NDCG返回。

还要注意每个排序请求list里面元素的数量要一样,不然ndcg_score函数会报错。

参考

本文链接

Leave a Comment

Your email address will not be published.