# 【机器学习】Rank 中使用的指标及示例代码

LogM

## 1. P@K

`P@K`，代表前 K 个预测值中有多少的准确率 (Precision)。

Prec@3 = 2/3

Prec@4 = 2/4

Prec@5 = 3/5

``````def precision(gt, pred, K):
""" Computes the average precision.
gt: list, ground truth, all relevant docs' index
pred: list, prediction
"""
hit_num = len(gt & set(pred[:K]))
return float(1.0 * hit_num / K)``````

## 2. MAP

`AP` 是 average precision 的缩写，计算方式是把所有相关文档的 `P@K` 求平均。

AP = (1/1 + 2/3 + 3/5) / 3 = 0.76

`AP` 会把所有"好的结果"都算上，但是排序模型可能会对某些"好的结果"漏召回，这些"漏召回"的 `P@K` 视为0。

`MAP` 是 mean average precision 的缩写，就是把所有的 `AP` 求平均。

``````def average_precision(gt, pred):
""" Computes the average precision.
gt: list, ground truth, all relevant docs' index
pred: list, prediction
"""
if not gt:
return 0.0

score = 0.0
num_hits = 0.0
for i, p in enumerate(pred):
if p in gt and p not in pred[:i]:
num_hits += 1.0
score += num_hits / (i + 1.0)

return score / max(1.0, len(gt))``````

## 3. MRR

RR = 1/2

``````def reciprocal_rank(gt, pred):
""" Computes the reciprocal rank.
gt: list, ground truth, all relevant docs' index
pred: list, prediction
"""
if not gt:
return 0.0

score = 0.0
for rank, item in enumerate(pred):
if item in gt:
score = 1.0 / (rank + 1.0)
break
return score``````

## 4. NDCG

`DCG`: Discounted Cumulative Gain

`IDCG`: ideal DCG，是 ground truth 情况下的 DCG

`NDCG`: normalized DCG，是 DCG 和 IDCG 相除

NDCG = DCG / IDCG

``````def NDCG(gt, pred, use_graded_scores=False):
""" Computes the NDCG.
gt: list, ground truth, all relevant docs' index
pred: list, prediction
"""
score = 0.0
for rank, item in enumerate(pred):
if item in gt:
grade = 1.0 / (gt.index(item) + 1)
else:
score += grade / np.log2(rank + 2)

norm = 0.0
for rank in range(len(gt)):