ndcg_score#
- sklearn.metrics.ndcg_score(y_true, y_score, *, k=None, sample_weight=None, ignore_ties=False)[源代码]#
计算归一化折损累积增益。
将根据预测分数排序的真实分数相加,并应用对数折损。然后除以最佳可能分数(理想DCG,通过完美排序获得),以得到0到1之间的分数。
如果真实标签被
y_score
排名靠前,此排名指标将返回高值。- 参数:
- y_true形状为 (n_samples, n_labels) 的类数组对象
多标签分类的真实目标,或待排序实体的真实分数。
y_true
中的负值可能导致输出不在0到1之间。- y_score形状为 (n_samples, n_labels) 的类数组对象
目标分数,可以是概率估计、置信值或未阈值化的决策度量(某些分类器上的“decision_function”返回的值)。
- kint,默认值=None
仅考虑排名中最高的 k 个分数。如果为
None
,则使用所有输出。- sample_weight形状为 (n_samples,) 的类数组对象,默认值=None
样本权重。如果为
None
,所有样本被赋予相同的权重。- ignore_tiesbool,默认值=False
为提高效率,假设 y_score 中没有并列项(如果 y_score 是连续的,这种情况很可能成立)。
- 返回:
- normalized_discounted_cumulative_gain浮点数,范围为 [0., 1.]
所有样本的平均NDCG分数。
另请参阅
dcg_score
折损累积增益(未归一化)。
参考文献
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 ndcg_score >>> # we have ground-truth relevance of some answers to a query: >>> true_relevance = np.asarray([[10, 0, 0, 1, 5]]) >>> # we predict some scores (relevance) for the answers >>> scores = np.asarray([[.1, .2, .3, 4, 70]]) >>> ndcg_score(true_relevance, scores) 0.69 >>> scores = np.asarray([[.05, 1.1, 1., .5, .0]]) >>> ndcg_score(true_relevance, scores) 0.49 >>> # we can set k to truncate the sum; only top k answers contribute. >>> ndcg_score(true_relevance, scores, k=4) 0.35 >>> # the normalization takes k into account so a perfect answer >>> # would still get 1.0 >>> ndcg_score(true_relevance, true_relevance, k=4) 1.0... >>> # 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 (normalized) >>> # true relevance of our top predictions: (10 / 10 + 5 / 10) / 2 = .75 >>> ndcg_score(true_relevance, scores, k=1) 0.75 >>> # 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: >>> ndcg_score(true_relevance, ... scores, k=1, ignore_ties=True) 0.5...