1

How can we calculte Mean Average Precision score in R ? Is there an easy way?

I calculate it as follows. I dont know if it is totally true or not..

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])
ibilgen
  • 460
  • 1
  • 7
  • 15
  • Take a look at http://en.wikipedia.org/wiki/Mean_average_precision#Mean_average_precision . It appears to be a straighforward calculation. Are you asking about MAP or about how to calculate the "average precision" for each query? – Carl Witthoft May 06 '13 at 11:43

2 Answers2

2

I add an example. This example assume that you have the real Y value as a vector of binary values and predicted Y value as a vector of continuous value.

# 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
Ray Gold
  • 168
  • 1
  • 9
1

Here's an example from the Metrics package.

Hack-R
  • 22,422
  • 14
  • 75
  • 131
TomHall
  • 286
  • 3
  • 15