从 R 中的混淆矩阵的结果计算精度、召回率和 FScore

ama*_*dia 5 r confusion-matrix precision-recall

我有以下混淆矩阵,现在我需要从中计算精度、召回率和 FScore,我如何使用获得的值来做到这一点?混淆矩阵和统计

      Reference
Prediction One Zero
      One   37   43
      Zero  19  131

               Accuracy : 0.7304          
                 95% CI : (0.6682, 0.7866)
    No Information Rate : 0.7565          
    P-Value [Acc > NIR] : 0.841087        

                  Kappa : 0.3611          
 Mcnemar's Test P-Value : 0.003489        

            Sensitivity : 0.6607          
            Specificity : 0.7529          
         Pos Pred Value : 0.4625          
         Neg Pred Value : 0.8733          
             Prevalence : 0.2435          
         Detection Rate : 0.1609          
   Detection Prevalence : 0.3478          
      Balanced Accuracy : 0.7068          

       'Positive' Class : One
Run Code Online (Sandbox Code Playgroud)

根据其他用户的建议,我使用了以下编辑过的代码

library(class)
library(e1071)
library(caret)
library(party)
library(nnet)
library(forecast)
pimad <- read.csv("C:/Users/USER/Desktop/AMAN/pimad.csv")
nrow(pimad)  
set.seed(9850)
gp<-runif(nrow(pimad))
pimad<-pimad[order(gp),]
idx <- createDataPartition(y = pimad$class, p = 0.7, list = FALSE)
train<-pimad[idx,]
test<-pimad[-idx,]
svmmodel<-svm(class~.,train,kernel="radial")
psvm<-predict(svmmodel,test)
table(psvm,test$class)
library(sos)
findFn("confusion matrix precision recall FScore")
df<-(confusionMatrix(test$class, psvm))
dim(df)
df[1,2]/sum(df[1,2:3])
df
Run Code Online (Sandbox Code Playgroud)

小智 6

无需执行其他任何操作,您已在 df 中获得了所有要求的度量。只需输入:

ls(df) [1] "byClass" "dots" "mode" "overall" "positive" "table"

df$byClass # 这是我研究过的另一个例子

现在所有参数,包括敏感性、特异性、pos pred val、neg pred val、精度、召回率、F1、流行率、检测率、检测流行率和平衡准确度都出现在一个表中


Dat*_*neR 1

嗯,这是对矩阵进行子集化的简单计算。

如果您的混淆矩阵被调用,请使用此处此处的df公式:

df
  Prediction One Zero
1        One  37   43
2       Zero  19  131

# Precision: tp/(tp+fp):
df[1,1]/sum(df[1,1:2])
[1] 0.4625

# Recall: tp/(tp + fn):
df[1,1]/sum(df[1:2,1])
[1] 0.6607143

# F-Score: 2 * precision * recall /(precision + recall):
2 * 0.4625 * 0.6607143 / (0.4625 + 0.6607143)
[1] 0.5441177
Run Code Online (Sandbox Code Playgroud)