榜单评估:AvgP@K 和 R@K 一样吗?
Evaluation of lists: AvgP@K and R@K are they same?
我的目标是理解平均值 Precision at K
和 Recall at K
。我有两个列表,一个是预测的,另一个是实际的(基本事实)
让我们将这两个列表称为预测的和实际的。现在我想做 precision@k
和 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
实际 = ['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)
上面提到的列表的最佳路线是什么?
我想,您已经检查过了 wiki。根据它的公式,第三个也是最大的一个(在单词 'This finite sum is equivalent to:' 之后),让我们看看每次迭代的示例:
- i=1 p=1
- i=2 相对 = 0
- i=3 p = 2/3
- i=4 p = 3/4
- i=5 相对 = 0
因此,avp@4 = avp@5 = (1 + 0.66 + 0.75) / 3 = 0.805; avp@3 = (1 + 0.66) / 3 依此类推。
召回@5 = 召回@4 = 3/3 = 1;召回@3 = 2/3;召回@2 =召回@1 = 1/3
下面是precision@k和recall@k的代码。我保留了你的符号,虽然使用 actual
表示 observed/returned 值和使用 expected
表示基本事实似乎更常见(例如,参见 JUnit 默认值)。
def precision(actual, predicted, k):
act_set = set(actual)
pred_set = set(predicted[:k])
result = len(act_set & pred_set) / float(k)
return result
def recall(actual, predicted, k):
act_set = set(actual)
pred_set = set(predicted[:k])
result = len(act_set & pred_set) / float(len(act_set))
return result
我的目标是理解平均值 Precision at K
和 Recall at K
。我有两个列表,一个是预测的,另一个是实际的(基本事实)
让我们将这两个列表称为预测的和实际的。现在我想做 precision@k
和 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
实际 = ['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)
上面提到的列表的最佳路线是什么?
我想,您已经检查过了 wiki。根据它的公式,第三个也是最大的一个(在单词 'This finite sum is equivalent to:' 之后),让我们看看每次迭代的示例:
- i=1 p=1
- i=2 相对 = 0
- i=3 p = 2/3
- i=4 p = 3/4
- i=5 相对 = 0
因此,avp@4 = avp@5 = (1 + 0.66 + 0.75) / 3 = 0.805; avp@3 = (1 + 0.66) / 3 依此类推。
召回@5 = 召回@4 = 3/3 = 1;召回@3 = 2/3;召回@2 =召回@1 = 1/3
下面是precision@k和recall@k的代码。我保留了你的符号,虽然使用 actual
表示 observed/returned 值和使用 expected
表示基本事实似乎更常见(例如,参见 JUnit 默认值)。
def precision(actual, predicted, k):
act_set = set(actual)
pred_set = set(predicted[:k])
result = len(act_set & pred_set) / float(k)
return result
def recall(actual, predicted, k):
act_set = set(actual)
pred_set = set(predicted[:k])
result = len(act_set & pred_set) / float(len(act_set))
return result