折损累积增益 (DCG) #
- sklearn.metrics.dcg_score(y_true, y_score, *, k=None, log_base=2, sample_weight=None, ignore_ties=False)[source]#
计算折损累积增益。
应用对数折扣后,对由预测得分引起的顺序排列的真实得分进行求和。
如果真实标签由
y_score
排名靠前,则此排名指标会产生较高的值。通常情况下,首选归一化折损累积增益 (NDCG,由ndcg_score计算)。
- 参数:
- y_truearray-like of shape (n_samples, n_labels)
多标签分类的真实目标,或待排序实体的真实分数。
- y_scorearray-like of shape (n_samples, n_labels)
目标分数,可以是概率估计、置信度值或非阈值决策度量(某些分类器返回的“decision_function”)。
- kint, default=None
仅考虑排名中最高的k个分数。如果为None,则使用所有输出。
- log_basefloat, default=2
用于折扣的对数底数。较低的值表示更尖锐的折扣(最高结果更重要)。
- sample_weightarray-like of shape (n_samples,), default=None
样本权重。如果为
None
,则所有样本都赋予相同的权重。- ignore_ties布尔值,默认值为False
为提高效率,假设y_score中不存在任何平局(如果y_score是连续的,则很可能如此)。
- 返回:
- discounted_cumulative_gain浮点数
平均样本DCG分数。
另请参见
ndcg_score
折扣累积增益除以理想折扣累积增益(完美排序获得的DCG),以便分数在0到1之间。
参考文献
Jarvelin, K., & Kekalainen, J. (2002). Cumulated gain-based evaluation of IR techniques. 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). A theoretical analysis of NDCG ranking measures. In Proceedings of the 26th Annual Conference on Learning Theory (COLT 2013).
McSherry, F., & Najork, M. (2008, March). Computing information retrieval performance measures efficiently in the presence of tied scores. In European conference on information retrieval (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) np.float64(9.49...) >>> # we can set k to truncate the sum; only top k answers contribute >>> dcg_score(true_relevance, scores, k=2) np.float64(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) np.float64(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) np.float64(5.0)