-
Notifications
You must be signed in to change notification settings - Fork 10
/
util.r
58 lines (52 loc) · 1.29 KB
/
util.r
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
output=function(y,treshold=.2){
y=y[order(y[,4],decreasing=TRUE),]
result=paste((ifelse(y[,4]>treshold,y[,1],'')),collapse=' ')
if(nchar(result)==0)
result=' '
result
}
#' Compute the average precision at k
#'
#' This function computes the average precision at k
#' between two sequences
#'
#' @param k max length of predicted sequence
#' @param actual ground truth set (vector)
#' @param predicted predicted sequence (vector)
#' @export
apk <- function(k, actual, predicted)
{
score <- 0.0
cnt <- 0.0
if(length(actual)==0)
return(0)
for (i in 1:min(k,length(predicted)))
{
if (predicted[i] %in% actual && !(predicted[i] %in% predicted[0:(i-1)]))
{
cnt <- cnt + 1
score <- score + cnt/i
}
}
score <- score / min(length(actual), k)
score
}
#' Compute the mean average precision at k
#'
#' This function computes the mean average precision at k
#' of two lists of sequences.
#'
#' @param k max length of predicted sequence
#' @param actual list of ground truth sets (vectors)
#' @param predicted list of predicted sequences (vectors)
#' @export
mapk <- function (k, actual, predicted)
{
scores <- rep(0, length(actual))
for (i in 1:length(scores))
{
scores[i] <- apk(k, actual[[i]], predicted[[i]])
}
score <- mean(scores)
score
}