计算recall和precision以及map代码
时间: 2024-01-09 17:10:08 浏览: 97
EvaluationMetrics:标准信息检索评估指标实施 - Recall、Precision 和 MAP
以下是计算recall、precision和map的Python代码:
```python
def recall(actual, predicted):
true_positives = len(set(actual) & set(predicted))
false_negatives = len(actual) - true_positives
return true_positives / (true_positives + false_negatives)
def precision(actual, predicted):
true_positives = len(set(actual) & set(predicted))
false_positives = len(predicted) - true_positives
return true_positives / (true_positives + false_positives)
def average_precision(actual, predicted):
score = 0.0
hits = 0.0
for i, p in enumerate(predicted):
if p in actual and p not in predicted[:i]:
hits += 1.0
score += hits / (i+1.0)
if hits == 0.0:
return 0.0
return score / hits
def mean_average_precision(actual, predicted):
return np.mean([average_precision(a, p) for a, p in zip(actual, predicted)])
```
其中,`actual`和`predicted`都是列表类型,分别表示实际值和预测值。`recall`和`precision`分别计算召回率和精确率,`average_precision`计算平均精度,`mean_average_precision`计算平均平均精度。
阅读全文