def dcg_k(r,k,method = 0):
'''
Score is discounted cumulative gain (dcg)
Relevance is positive real values. Can use binary
as the previous methods.
Example from
http://www.stanford.edu/class/cs276/handouts/EvaluationNew-handout-6-per.pdf
Parameters
----------
r: Relevance scores (list or numpy) in rank order
(first element is the first item)
k: Number of results to consider
method: 0 or 1
Returns
-------
Discounted cumulative gain
'''
r = np.asfarray(r)[:k]
if r.size:
if method == 0:
#standard definition
return r[0] + np.sum(r[1:] / np.log2(np.arange(2, r.size + 1)))
elif method == 1:
#used in Kaggle
return np.sum((np.power(2,r) - 1.0) / np.log2(np.arange(2, r.size + 2)))
else:
raise ValueError('method must be 0 or 1.')
return 0.
评论列表
文章目录