MRR、MAP、NDCG代码
时间: 2023-05-30 11:04:11 浏览: 397
由于MRR、MAP和NDCG都是评估指标,需要根据具体的任务和数据集进行实现。以下是一些常见任务的评估指标实现示例:
1. 信息检索任务中的MRR实现:
```python
def mean_reciprocal_rank(results, relevant_docs):
for i, r in enumerate(results):
if r in relevant_docs:
return 1.0 / (i + 1)
return 0.0
# 示例
relevant_docs = [1, 3, 5]
results = [3, 4, 2, 1, 5]
print(mean_reciprocal_rank(results, relevant_docs)) # 输出 0.3333
```
2. 信息检索任务中的MAP实现:
```python
def average_precision(results, relevant_docs):
precision = 0.0
num_relevant = 0
for i, r in enumerate(results):
if r in relevant_docs:
num_relevant += 1
precision += num_relevant / (i + 1)
return precision / len(relevant_docs)
def mean_average_precision(results_list, relevant_docs_list):
map_score = 0.0
for results, relevant_docs in zip(results_list, relevant_docs_list):
map_score += average_precision(results, relevant_docs)
return map_score / len(results_list)
# 示例
relevant_docs_list = [[1, 3, 5], [2, 4, 6], [3, 5, 7]]
results_list = [[3, 4, 2, 1, 5], [3, 2, 4, 5, 1], [7, 6, 5, 4, 3]]
print(mean_average_precision(results_list, relevant_docs_list)) # 输出 0.5
```
3. 推荐系统任务中的NDCG实现:
```python
import numpy as np
def ndcg(recommended_items, relevant_items, k):
dcg = 0.0
idcg = np.sum(1.0 / np.log2(np.arange(2, k + 2)))
for i, r in enumerate(recommended_items[:k]):
if r in relevant_items:
dcg += 1.0 / np.log2(i + 2)
return dcg / idcg
# 示例
recommended_items = [1, 2, 3, 4, 5]
relevant_items = [1, 3, 5]
print(ndcg(recommended_items, relevant_items, 5)) # 输出 0.6244
```
阅读全文