#已经取了前k个
def precision_and_recall(ranked_list, ground_list):hits = 0for i in range(len(ranked_list)):id = ranked_list[i]if id in ground_list:hits += 1pre = hits / (1.0 * len(ranked_list) if len(ground_list) != 0 else 1)rec = hits / (1.0 * len(ground_list) if len(ground_list) != 0 else 1)return pre, recdef IDCG(n):idcg = 0for i in range(n):idcg += 1 / math.log(i + 2, 2)return idcgdef nDCG(ranked_list, ground_truth):dcg = 0idcg = IDCG(len(ground_truth))for i in range(len(ranked_list)):id = ranked_list[i]if id not in ground_truth:continuerank = i + 1dcg += 1 / math.log(rank + 1, 2)return dcg / idcg if idcg != 0 else 0def AP(ranked_list, ground_truth):hits, sum_precs = 0, 0.0for i in range(len(ranked_list)):id = ranked_list[i]if id in ground_truth:hits += 1sum_precs += hits / (i + 1.0)if hits > 0:return sum_precs / len(ground_truth)else:return 0.0def RR(ranked_list, ground_list):for i in range(len(ranked_list)):id = ranked_list[i]if id in ground_list:return 1 / (i + 1.0)return 0
2
def precision_recall_ndcg_at_k(k, rankedlist, test_matrix):idcg_k = 0dcg_k = 0n_k = k if len(test_matrix) > k else len(test_matrix)for i in range(n_k):idcg_k += 1 / math.log(i + 2, 2)b1 = rankedlistb2 = test_matrixs2 = set(b2)hits = [ (idx, val) for idx, val in enumerate(b1) if val in s2]count = len(hits)#print(hits)r = np.array(rankedlist)for c in range(count):dcg_k += 1 / math.log(hits[c][0] + 2, 2)#print(float(count / k))#print(float(count / len(test_matrix)))return float(count / k), float(count / len(test_matrix)), float(dcg_k / idcg_k)def map_mrr_ndcg(rankedlist, test_matrix):ap = 0map = 0dcg = 0idcg = 0mrr = 0for i in range(len(test_matrix)):idcg += 1 / math.log(i + 2, 2)b1 = rankedlistb2 = test_matrixs2 = set(b2)hits = [ (idx, val) for idx, val in enumerate(b1) if val in s2]count = len(hits)for c in range(count):ap += (c+1) / (hits[c][0] + 1)dcg += 1 / math.log(hits[c][0] + 2, 2)if count != 0:mrr = 1 / (hits[0][0] + 1)if count != 0:map = ap / countreturn map, mrr, float(dcg / idcg)