dcg_score#

sklearn.metrics.dcg_score(y_true, y_score, *, k=None, log_base=2, sample_weight=None, ignore_ties=False)[來源]#

計算折扣累積增益。

在應用對數折扣後,將由預測分數引起的順序中排序的真實分數加總。

如果真實標籤由 y_score 排序較高,則此排序度量會產生較高的值。

通常首選標準化折扣累積增益(NDCG,由 ndcg_score 計算)。

參數
y_true形狀為 (n_samples, n_labels) 的類陣列

多標籤分類的真實目標值,或是要排序的實體的真實分數。

y_score形狀為 (n_samples, n_labels) 的類陣列

目標分數,可以是機率估計值、信賴度值,或是未經閾值處理的決策度量(由某些分類器的「decision_function」返回)。

k整數,預設為 None

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

log_base浮點數,預設為 2

折扣所使用的對數底數。較低的值表示較陡峭的折扣(頂部結果更重要)。

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

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

ignore_ties布林值,預設為 False

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

返回
discounted_cumulative_gain浮點數

平均樣本 DCG 分數。

另請參閱

ndcg_score

折扣累積增益除以理想折扣累積增益(完美排序獲得的 DCG),以便分數介於 0 和 1 之間。

參考文獻

Wikipedia 關於折扣累積增益的條目.

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)