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

后端 未结 7 1426
不思量自难忘°
不思量自难忘° 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:19

    Just to update this as I came across this thread now, the confusionMatrix function in caretcomputes all of these things for you automatically.

    cm <- confusionMatrix(prediction, reference = test_set$label)
    
    # extract F1 score for all classes
    cm[["byClass"]][ , "F1"] #for multiclass classification problems
    

    You can substitute any of the following for "F1" to extract the relevant values as well:

    "Sensitivity", "Specificity", "Pos Pred Value", "Neg Pred Value", "Precision", "Recall", "F1", "Prevalence", "Detection", "Rate", "Detection Prevalence", "Balanced Accuracy"

    I think this behaves slightly differently when you're only doing a binary classifcation problem, but in both cases, all of these values are computed for you when you look inside the confusionMatrix object, under $byClass

提交回复
热议问题