ndcg 分數#

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」返回的未經閾值處理的決策度量。

k整數,預設為 None

僅考慮排名中最高的 k 個分數。如果為 None,則使用所有輸出。

sample_weight類陣列,形狀為 (n_samples,),預設為 None

樣本權重。如果為 None,則所有樣本將被給予相同的權重。

ignore_ties布林值,預設為 False

假設 y_score 中沒有並列的情況(如果 y_score 是連續的,則很可能是這種情況),以提高效率。

回傳值:
normalized_discounted_cumulative_gain介於 [0., 1.] 之間的浮點數

所有樣本的平均 NDCG 分數。

另請參閱

dcg_score

折現累計增益 (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.

範例

>>> 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)
np.float64(0.69...)
>>> scores = np.asarray([[.05, 1.1, 1., .5, .0]])
>>> ndcg_score(true_relevance, scores)
np.float64(0.49...)
>>> # we can set k to truncate the sum; only top k answers contribute.
>>> ndcg_score(true_relevance, scores, k=4)
np.float64(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)
np.float64(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)
np.float64(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)
np.float64(0.5...)