#NEWLINE# def dcg_at_k( r , k , method = 1 ) : #NEWLINE# #INDENT# 'Score is discounted cumulative gain (dcg)\n\n\tRelevance is positive real values.  Can use binary\n\tas the previous methods.\n\n\tExample from\n\thttp://www.stanford.edu/class/cs276/handouts/EvaluationNew-handout-6-per.pdf\n\t>>> r = [3, 2, 3, 0, 0, 1, 2, 2, 3, 0]\n\t>>> dcg_at_k(r, 1)\n\t3.0\n\t>>> dcg_at_k(r, 1, method=1)\n\t3.0\n\t>>> dcg_at_k(r, 2)\n\t5.0\n\t>>> dcg_at_k(r, 2, method=1)\n\t4.2618595071429155\n\t>>> dcg_at_k(r, 10)\n\t9.6051177391888114\n\t>>> dcg_at_k(r, 11)\n\t9.6051177391888114\n\n\tArgs:\n\t\tr: Relevance scores (list or numpy) in rank order\n\t\t\t(first element is the first item)\n\t\tk: Number of results to consider\n\t\tmethod: If 0 then weights are [1.0, 1.0, 0.6309, 0.5, 0.4307, ...]\n\t\t\t\tIf 1 then weights are [1.0, 0.6309, 0.5, 0.4307, ...]\n\n\tReturns:\n\t\tDiscounted cumulative gain\n\t' #NEWLINE# r = np . asfarray ( r ) [ : k ] #NEWLINE# if r . size : #NEWLINE# #INDENT# if ( method == 0 ) : #NEWLINE# #INDENT# return ( r [ 0 ] + np . sum ( ( r [ 1 : ] / np . log2 ( np . arange ( 2 , ( r . size + 1 ) ) ) ) ) ) #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# if ( k == 1 ) : #NEWLINE# #INDENT# return np . sum ( ( r / denominator_table [ : r . shape [ 0 ] ] ) ) #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# raise ValueError ( 'method must be 0 or 1.' ) #NEWLINE# #UNINDENT# #UNINDENT# #UNINDENT# return 0.0