dcg_score#
- sklearn.metrics.dcg_score(y_true, y_score, *, k=None, log_base=2, sample_weight=None, ignore_ties=False)[source]#
计算折损累计增益(Discounted Cumulative Gain)。
对根据预测得分排序的真实得分进行求和,并应用对数折损。
如果真实标签被
y_score
排在靠前位置,则此排序指标会产生高值。通常更推荐使用标准化折损累计增益(Normalized Discounted Cumulative Gain, NDCG,通过 ndcg_score 计算)。
- 参数:
- y_true形状为 (n_samples, n_labels) 的类数组
多标签分类的真实目标,或待排序实体的真实得分。
- y_score形状为 (n_samples, n_labels) 的类数组
目标得分,可以是概率估计、置信度值,或未经过阈值处理的决策度量(某些分类器上的“decision_function”返回的值)。
- k整型, 默认为 None
仅考虑排序中最高的 k 个得分。如果为 None,则使用所有输出。
- log_base浮点型, 默认为 2
用于折损的对数基数。值越低表示折损越急剧(靠前结果越重要)。
- sample_weight形状为 (n_samples,) 的类数组, 默认为 None
样本权重。如果为
None
,则所有样本具有相同的权重。- ignore_ties布尔型, 默认为 False
为提高效率,假设 y_score 中没有平局(如果 y_score 是连续的,则很可能如此)。
- 返回:
- discounted_cumulative_gain浮点型
平均样本 DCG 分数。
另请参阅
ndcg_score
折损累计增益除以理想折损累计增益(完美排名获得的 DCG),以得到一个介于 0 和 1 之间的分数。
参考文献
Jarvelin, K., & Kekalainen, J. (2002). 基于累计增益的IR技术评估。ACM Transactions on Information Systems (TOIS), 20(4), 422-446。
Wang, Y., Wang, L., Li, Y., He, D., Chen, W., & Liu, T. Y. (2013, May). NDCG 排名度量的理论分析。收录于第26届学习理论年会论文集 (COLT 2013)。
McSherry, F., & Najork, M. (2008, March). 在存在平局得分的情况下高效计算信息检索性能度量。收录于欧洲信息检索会议 (pp. 414-421)。Springer, Berlin, Heidelberg。
示例
>>> import numpy as np >>> from sklearn.metrics import dcg_score >>> # we have ground-truth relevance of some answers to a query: >>> true_relevance = np.asarray([[10, 0, 0, 1, 5]]) >>> # we predict scores for the answers >>> scores = np.asarray([[.1, .2, .3, 4, 70]]) >>> dcg_score(true_relevance, scores) 9.49 >>> # we can set k to truncate the sum; only top k answers contribute >>> dcg_score(true_relevance, scores, k=2) 5.63 >>> # now we have some ties in our prediction >>> scores = np.asarray([[1, 0, 0, 0, 1]]) >>> # by default ties are averaged, so here we get the average true >>> # relevance of our top predictions: (10 + 5) / 2 = 7.5 >>> dcg_score(true_relevance, scores, k=1) 7.5 >>> # we can choose to ignore ties for faster results, but only >>> # if we know there aren't ties in our scores, otherwise we get >>> # wrong results: >>> dcg_score(true_relevance, ... scores, k=1, ignore_ties=True) 5.0