我的目标是了解 AveragePrecision at K
和Recall at K
. 我有两个列表,一个是预测的,另一个是实际的(基本事实)
让我们将这两个列表称为预测的和实际的。现在我想做precision@k
and recall@k
。
使用 python,我在 K 处实现了 Avg 精度,如下所示:
def apk(actual, predicted, k=10):
"""
Computes the average precision at k.
This function computes the average precision at k between two lists of items.
Parameters
----------
actual: list
A list of elements that are to be predicted (order doesn't matter)
predicted : list
A list of predicted elements (order does matter)
k: int, optional
Returns
-------
score : double
The average precision at k over the input lists
"""
if len(predicted) > k:
predicted = predicted[:k]
score = 0.0
num_hits = 0.0
for i,p in enumerate(predicted):
if p in actual and p not in predicted[:i]:
num_hits += 1.0
score += num_hits / (i + 1.0)
if not actual:
return 1.0
if min(len(actual), k) == 0:
return 0.0
else:
return score / min(len(actual), k)
假设我们的预测有 5 个字符串,顺序如下:
predicted = ['b','c','a','e','d'] and
actual = ['a','b','e'] since we are doing @k would the precision@k is same as
recall@k ? If not how would I do
recall@k`
如果我想做f-measure (f-score)
上述列表的最佳途径是什么?