Skip to content

Instantly share code, notes, and snippets.

@mbq
Created December 29, 2016 14:46
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mbq/c06ae33860b1131ce57672a323382692 to your computer and use it in GitHub Desktop.
Save mbq/c06ae33860b1131ce57672a323382692 to your computer and use it in GitHub Desktop.
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