sklearn.metrics.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 ndarray, shape (n_samples, n_labels)
多標簽分類的真實目標,或要排名的實體的真實分數。
y_score ndarray, shape (n_samples, n_labels)
目標分數可以是概率估計,置信度值或決策的非閾值度量(如某些分類器上的“ decision_function”所返回)。
k int, optional (default=None)
僅考慮排名中最高的k分數。 如果為None,則使用所有輸出。
sample_weight ndarray, shape (n_samples,), optional (default=None)
樣本權重。如果為None,則所有樣本的權重都相同。
ignore_ties bool, optional (default=False)
假設y_score在效率增益方面不存在關聯(如果y_score是連續的,則可能是這種情況)。
返回值 說明
normalized_discounted_cumulative_gain float in [0., 1.]
所有樣本的平均NDCG分數。

另見:

dcg_score

? 折損累計增益(未歸一化)。

參考

Wikipedia entry for Discounted Cumulative Gain

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.

示例

>>> from sklearn.metrics import ndcg_score
>>> # we have groud-truth relevance of some answers to a query:
>>> true_relevance = np.asarray([[100015]])
>>> # we predict some scores (relevance) for the answers
>>> scores = np.asarray([[.1.2.3470]])
>>> ndcg_score(true_relevance, scores)
0.69...
>>> scores = np.asarray([[.051.11..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([[10001]])
>>> # 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