【问题标题】:mean average precision in RR中的平均平均精度
【发布时间】:2018-01-19 01:42:18
【问题描述】:

我们如何计算 R 中的平均精度分数?有没有简单的方法?

我计算如下。不知道是不是真的。。

pr = prediction(preds, labs)
pf = performance(pr, "prec", "rec")
# plot(pf)

pf@x.name
 [1] "Recall"

pf@y.name
 [1] "Precision"

rec = pf@x.values[[1]]

prec = pf@y.values[[1]]

idxall = NULL
for(i in 1:10){
  i = i/10

  # find closest values in recall to the values 0, 0.1, 0.2, ... ,1.0
  idx = which(abs(rec-i)==min(abs(rec-i)))

  # there are more than one value return, choose the value in the middle
  idx = idx[ceiling(length(idx)/2)] 

  idxall = c(idxall, idx)
}

prec.mean = mean(prec[idxall])

【问题讨论】:

  • 看看en.wikipedia.org/wiki/…。这似乎是一个直截了当的计算。您是在询问 MAP 还是关于如何计算每个查询的“平均精度”?

标签: r average-precision


【解决方案1】:

我添加一个例子。 此示例假设您将实际 Y 值作为二进制值向量,将预测 Y 值作为连续值向量。

# vbYreal: real Y values
# vdYhat: predicted Y values
# ex) uNumToExamineK <- length(vbYreal)
#     vbYreal <- c(1,0,1,0,0,1,0,0,1,1,0,0,0,0,0)
#     vdYhat <- c(.91, .89, .88, .85, .71, .70, .6, .53, .5, .4, .3, .3, .3, .3, .1)
# description:
# vbYreal_sort_d is the descending order of vbYreal(e.g.,     c(1,0,1,0,0,1,0,0,1,1,0,0,0,0,0) )
FuAPk <- function (uNumToExamineK, vbYreal, vdYhat){

  # The real Y values is sorted by predicted Y values in decending order(decreasing=TRUE) 
  vbYreal_sort_d <- vbYreal[order(vdYhat, decreasing=TRUE)]
  vbYreal_sort_d <- vbYreal_sort_d[1:uNumToExamineK]
  uAveragePrecision <- sum(cumsum(vbYreal_sort_d) * vbYreal_sort_d / seq_along(vbYreal_sort_d)) /
    sum(vbYreal_sort_d)
  uAveragePrecision
}

vbYreal <- c(1,0,1,0,0,1,0,0,1,1,0,0,0,0,0)
vdYhat <- c(.91, .89, .88, .85, .71, .70, .6, .53, .5, .4, .3, .3, .3, .3, .1)

FuAPk(length(vbYreal), vbYreal, vdYhat)
# [1] 0.6222222

【讨论】:

    【解决方案2】:

    HereMetrics 包中的一个示例。

    【讨论】:

      猜你喜欢
      • 2020-09-30
      • 2017-04-15
      • 2018-01-14
      • 2019-04-09
      • 2015-01-09
      • 2012-11-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多