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)
[email protected]
[1] "Recall"
[email protected]
[1] "Precision"
rec = [email protected][[1]]
prec = [email protected][[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])
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.