Adapted to Movie lens dataset
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

scorer.py 2.1KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. import math
  2. def AP(ranked_list, ground_truth, topn):
  3. hits, sum_precs = 0, 0.0
  4. t = [a for a in ground_truth]
  5. t.sort(reverse=True)
  6. t=t[:topn]
  7. for i in range(topn):
  8. id = ranked_list[i]
  9. if ground_truth[id] in t:
  10. hits += 1
  11. sum_precs += hits / (i+1.0)
  12. t.remove(ground_truth[id])
  13. if hits > 0:
  14. return sum_precs / topn
  15. else:
  16. return 0.0
  17. def RR(ranked_list, ground_truth,topn):
  18. t = [a for a in ground_truth]
  19. t.sort(reverse=True)
  20. t = t[:topn]
  21. for i in range(topn):
  22. id = ranked_list[i]
  23. if ground_truth[id] in t:
  24. return 1 / (i + 1.0)
  25. return 0
  26. def precision(ranked_list,ground_truth,topn):
  27. t = [a for a in ground_truth]
  28. t.sort(reverse=True)
  29. t = t[:topn]
  30. hits = 0
  31. for i in range(topn):
  32. id = ranked_list[i]
  33. if ground_truth[id] in t:
  34. t.remove(ground_truth[id])
  35. hits += 1
  36. pre = hits/topn
  37. return pre
  38. def nDCG(ranked_list, ground_truth, topn):
  39. dcg = 0
  40. idcg = IDCG(ground_truth, topn)
  41. # print(ranked_list)
  42. # input()
  43. for i in range(topn):
  44. id = ranked_list[i]
  45. dcg += ((2 ** ground_truth[id]) -1)/ math.log(i+2, 2)
  46. # print('dcg is ', dcg, " n is ", topn)
  47. # print('idcg is ', idcg, " n is ", topn)
  48. return dcg / idcg
  49. def IDCG(ground_truth,topn):
  50. t = [a for a in ground_truth]
  51. t.sort(reverse=True)
  52. idcg = 0
  53. for i in range(topn):
  54. idcg += ((2**t[i]) - 1) / math.log(i+2, 2)
  55. return idcg
  56. def add_metric(recommend_list, ALL_group_list, precision_list, ap_list, ndcg_list, topn):
  57. ndcg = nDCG(recommend_list, ALL_group_list, topn)
  58. ap = AP(recommend_list, ALL_group_list, topn)
  59. pre = precision(recommend_list, ALL_group_list, topn)
  60. precision_list.append(pre)
  61. ap_list.append(ap)
  62. ndcg_list.append(ndcg)
  63. def cal_metric(precision_list,ap_list,ndcg_list):
  64. mpre = sum(precision_list) / len(precision_list)
  65. map = sum(ap_list) / len(ap_list)
  66. mndcg = sum(ndcg_list) / len(ndcg_list)
  67. return mpre, mndcg, map