Skip to content

Commit 6289093

Browse files
author
shixiaowen03
committed
recommendation evaluation metrics
1 parent 6e6ad9e commit 6289093

6 files changed

Lines changed: 346 additions & 82 deletions

File tree

.idea/workspace.xml

Lines changed: 277 additions & 82 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
import numpy as np
2+
3+
4+
label_all = np.random.randint(0,2,[10,1]).tolist()
5+
pred_all = np.random.random((10,1)).tolist()
6+
7+
print(label_all)
8+
print(pred_all)
9+
10+
posNum = len(list(filter(lambda s: s[0] == 1, label_all)))
11+
12+
if (posNum > 0):
13+
negNum = len(label_all) - posNum
14+
sortedq = sorted(enumerate(pred_all), key=lambda x: x[1])
15+
16+
posRankSum = 0
17+
for j in range(len(pred_all)):
18+
if (label_all[j][0] == 1):
19+
posRankSum += list(map(lambda x: x[0], sortedq)).index(j) + 1
20+
auc = (posRankSum - posNum * (posNum + 1) / 2) / (posNum * negNum)
21+
print("auc:", auc)
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
def hit(gt_items, pred_items):
2+
count = 0
3+
for item in pred_items:
4+
if item in gt_items:
5+
count += 1
6+
return count
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
def AP(ranked_list, ground_truth):
2+
"""Compute the average precision (AP) of a list of ranked items
3+
4+
"""
5+
hits = 0
6+
sum_precs = 0
7+
for n in range(len(ranked_list)):
8+
if ranked_list[n] in ground_truth:
9+
hits += 1
10+
sum_precs += hits / (n + 1.0)
11+
if hits > 0:
12+
return sum_precs / len(ground_truth)
13+
else:
14+
return 0
15+
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
import numpy as np
2+
3+
4+
def mrr(gt_items, pred_items):
5+
for index,item in enumerate(pred_items):
6+
if item in gt_items:
7+
return 1/index
8+
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
import numpy as np
2+
3+
def ndcg(pred_rel):
4+
dcg = 0
5+
for (index,rel) in enumerate(pred_rel):
6+
dcg += (rel * np.reciprocal(np.log2(index+2)))
7+
print("dcg " + str(dcg))
8+
idcg = 0
9+
for(index,rel) in enumerate(sorted(pred_rel,reverse=True)):
10+
idcg += (rel * np.reciprocal(np.log2(index+2)))
11+
print("idcg " + str(idcg))
12+
13+
14+
return dcg/idcg
15+
16+
17+
pred_rel = [3,1,2,3,2]
18+
print("ndcg " + str(ndcg(pred_rel)))
19+

0 commit comments

Comments
 (0)