2013-05-06 117 views
0

如何计算R中的平均精确度平均精度指数?有没有简单的方法?R中的平均精确度精度

我计算如下。我不知道这是否是完全正确或不..

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]) 
+0

看看http://en.wikipedia.org/wiki/Mean_average_precision#Mean_average_precision。这似乎是一个直截了当的计算。您是在询问MAP还是关于如何计算每个查询的“平均精度”? – 2013-05-06 11:43:40

回答

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