Easy way of counting precision, recall and F1-score in R

后端 未结 7 1278
不思量自难忘°
不思量自难忘° 2021-01-31 04:45

I am using an rpart classifier in R. The question is - I would want to test the trained classifier on a test data. This is fine - I can use the predict.rpart<

7条回答
  •  后悔当初
    2021-01-31 05:27

    I noticed the comment about F1 score being needed for binary classes. I suspect that it usually is. But a while ago I wrote this in which I was doing classification into several groups denoted by number. This may be of use to you...

    calcF1Scores=function(act,prd){
      #treats the vectors like classes
      #act and prd must be whole numbers
      df=data.frame(act=act,prd=prd);
      scores=list();
      for(i in seq(min(act),max(act))){
        tp=nrow(df[df$prd==i & df$act==i,]);        
        fp=nrow(df[df$prd==i & df$act!=i,]);
        fn=nrow(df[df$prd!=i & df$act==i,]);
        f1=(2*tp)/(2*tp+fp+fn)
        scores[[i]]=f1;
      }      
      print(scores)
      return(scores);
    }
    
    print(mean(unlist(calcF1Scores(c(1,1,3,4,5),c(1,2,3,4,5)))))
    print(mean(unlist(calcF1Scores(c(1,2,3,4,5),c(1,2,3,4,5)))))
    

提交回复
热议问题