dcg_score#
- 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_true形状为 (n_samples, n_labels) 的类数组对象
多标签分类的真实目标,或待排名的实体的真实分数。
- y_score形状为 (n_samples, n_labels) 的类数组对象
目标分数,可以是概率估计、置信值,或未经过阈值处理的决策度量(如某些分类器返回的“decision_function”)。
- kint, default=None
仅考虑排名中最高的 k 个分数。如果为 None,则使用所有输出。
- log_basefloat, default=2
用于折扣计算的对数底数。值越低意味着折扣越陡峭(顶部结果越重要)。
- sample_weightshape 为 (n_samples,) 的 array-like, default=None
样本权重。如果为
None,则所有样本具有相同的权重。- ignore_tiesbool, default=False
假设 y_score 中没有平局(如果 y_score 是连续的,这种情况很可能发生),以提高效率。
- 返回:
- discounted_cumulative_gainfloat
平均的样本 DCG 分数。
另请参阅
ndcg_score折损累计增益除以理想折损累计增益(完美排名获得的 DCG),以便得到一个介于 0 和 1 之间的分数。
References
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) 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