推荐算法topk评价指标代码
时间: 2023-06-22 22:10:47 浏览: 151
推荐算法的topk评价指标有很多,常用的有Precision@k、Recall@k、F1@k、Mean Average Precision (MAP)、Normalized Discounted Cumulative Gain (NDCG)等。这里给出Precision@k和Recall@k的Python代码实现。
假设推荐算法的结果存储在一个列表recommendations中,每个元素表示一条推荐记录,包含用户ID和推荐的物品ID列表。ground_truth是一个字典,包含用户ID和实际喜欢的物品ID列表。
```python
# 计算Precision@k
def precision_at_k(recommendations, ground_truth, k):
p_sum = 0.0
for user_id, item_list in ground_truth.items():
rec_list = [rec[1] for rec in recommendations if rec[0] == user_id][:k]
hits = len(set(rec_list) & set(item_list))
p_sum += hits / k
return p_sum / len(ground_truth)
# 计算Recall@k
def recall_at_k(recommendations, ground_truth, k):
r_sum = 0.0
for user_id, item_list in ground_truth.items():
rec_list = [rec[1] for rec in recommendations if rec[0] == user_id][:k]
hits = len(set(rec_list) & set(item_list))
r_sum += hits / len(item_list)
return r_sum / len(ground_truth)
```
其中,recommendations和ground_truth的格式如下:
```python
recommendations = [(user_id1, [item_id1, item_id2, ...]), (user_id2, [item_id1, item_id2, ...]), ...]
ground_truth = {user_id1: [item_id1, item_id2, ...], user_id2: [item_id1, item_id2, ...], ...}
```
使用方法如下:
```python
# 示例数据
recommendations = [(1, [101, 102, 103]), (2, [201, 203, 204]), (3, [301, 302, 304])]
ground_truth = {1: [101, 104], 2: [201, 202, 203], 3: [301, 302, 303]}
# 计算Precision@2和Recall@2
p_at_2 = precision_at_k(recommendations, ground_truth, 2)
r_at_2 = recall_at_k(recommendations, ground_truth, 2)
print('Precision@2:', p_at_2)
print('Recall@2:', r_at_2)
```
阅读全文