Create a gist now

Instantly share code, notes, and snippets.

@mbq /prf.R
Created Dec 29, 2016

Precision, recall and F-score one-liners
precision<-function(y,p) sum(y&p)/sum(p)
recall<-function(y,p) sum(y&p)/sum(y)
Fscore<-function(y,p) 2/(1+sum(y|p)/sum(y&p))
precisionS<-function(Y,P) mean(P%in%Y)
recallS<-function(Y,P) mean(Y%in%P)
FscoreS<-function(Y,P) mean(c(Y,P)%in%intersect(P,Y))
test<-function(N=sample(10:1000,1)){
y<-runif(N)>runif(1);
p<-runif(N)>runif(1);
c(precision(y,p),recall(y,p),Fscore(y,p))->A;
Y<-which(y);which(p)->P;
c(precisionS(Y,P),recallS(Y,P),FscoreS(Y,P))->B;
stopifnot(all.equal(A,B))
return(rbind(A,B))
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment