首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >为什么一些准确性指标没有显示在插入符号中( F1、召回和精度)

为什么一些准确性指标没有显示在插入符号中( F1、召回和精度)
EN

Stack Overflow用户
提问于 2021-06-11 19:48:52
回答 1查看 34关注 0票数 0

下午好,

假设我们有以下内容:

代码语言:javascript
复制
pred=c(2, 1, 1, 1, 1, 2, 1, 1, 1, 1, 2, 1, 1, 1, 1, 1, 1, 1, 1, 1)

obs=structure(c(2L, 1L, 1L, 1L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 
1L, 1L, 1L, 1L, 1L, 2L, 1L), .Label = c("no-recurrence-events", 
"recurrence-events"), class = "factor")

和以下函数:

代码语言:javascript
复制
library(plyr)
library(caret)

metrics<-function(observed1,predicted1){
  
  #predicted1=mapvalues(predicted1, from = unique(predicted1), to = unique(observed1))
  observed1=mapvalues(observed1, from = unique(observed1), to = unique(predicted1) )
  #xtab=table(observed1, predicted1)
  xtab=table(predicted1,observed1)
  #result<- confusionMatrix(as.factor(predicted1), as.factor(observed1))
  result<- confusionMatrix(as.factor(predicted1),as.factor(observed1))
  print(recall(as.factor(predicted1), as.factor(observed1)))  # i can print recall 0.9285714
  return(list(xtab,result))
  
  
}

运行后,F1、召回率和精确度等指标没有显示出来:

代码语言:javascript
复制
metrics(obs,pred)

[[1]]
          observed1
predicted1  1  2
         1 13  4
         2  1  2

[[2]]
Confusion Matrix and Statistics

          Reference
Prediction  1  2
         1 13  4
         2  1  2
                                         
               Accuracy : 0.75           
                 95% CI : (0.509, 0.9134)
    No Information Rate : 0.7            
    P-Value [Acc > NIR] : 0.4164         
                                         
                  Kappa : 0.3056         
                                         
 Mcnemar's Test P-Value : 0.3711         
                                         
            Sensitivity : 0.9286         
            Specificity : 0.3333         
         Pos Pred Value : 0.7647         
         Neg Pred Value : 0.6667         
             Prevalence : 0.7000         
         Detection Rate : 0.6500         
   Detection Prevalence : 0.8500         
      Balanced Accuracy : 0.6310         
                                         
       'Positive' Class : 1   

pb是一些准确性指标没有显示出来,这看起来很奇怪: F1,召回,精度!

我注意到了另一个问题,例如,如果我们颠倒顺序:

代码语言:javascript
复制
metrics(pred,obs)

Error in confusionMatrix.default(as.factor(predicted1), as.factor(observed1)) : 
  The data must contain some levels that overlap the reference.
Calls: metrics -> confusionMatrix -> confusionMatrix.default
Execution halted

我希望我的问题是清楚的。

感谢您的帮助!

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2021-06-11 21:53:05

我找到了一个解决方案。confusionMatrix()有一个名为mode='everything'的选项,可以输出所有已实现的度量:

代码语言:javascript
复制
pred=c(2, 1, 1, 1, 1, 2, 1, 1, 1, 1, 2, 1, 1, 1, 1, 1, 1, 1, 1, 1)

obs=structure(c(2L, 1L, 1L, 1L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 
1L, 1L, 1L, 1L, 1L, 2L, 1L), .Label = c("no-recurrence-events", 
"recurrence-events"), class = "factor")

library(plyr)
library(caret)

metrics<-function(observed1,predicted1){
  
  #predicted1=mapvalues(predicted1, from = unique(predicted1), to = unique(observed1))
  observed1=mapvalues(observed1, from = unique(observed1), to = unique(predicted1) )
  #xtab=table(observed1, predicted1)
  xtab=table(predicted1,observed1)
  #result<- confusionMatrix(as.factor(predicted1), as.factor(observed1))
  result<- confusionMatrix(as.factor(predicted1),as.factor(observed1),mode='everything')
  return(list(xtab,result))
  
  
}

metrics(obs,pred)

这给出了预期的输出:

代码语言:javascript
复制
Loading required package: lattice
Loading required package: ggplot2
[[1]]
          observed1
predicted1  1  2
         1 13  4
         2  1  2

[[2]]
Confusion Matrix and Statistics

          Reference
Prediction  1  2
         1 13  4
         2  1  2
                                         
               Accuracy : 0.75           
                 95% CI : (0.509, 0.9134)
    No Information Rate : 0.7            
    P-Value [Acc > NIR] : 0.4164         
                                         
                  Kappa : 0.3056         
                                         
 Mcnemar's Test P-Value : 0.3711         
                                         
            Sensitivity : 0.9286         
            Specificity : 0.3333         
         Pos Pred Value : 0.7647         
         Neg Pred Value : 0.6667         
              Precision : 0.7647         
                 Recall : 0.9286         
                     F1 : 0.8387         
             Prevalence : 0.7000         
         Detection Rate : 0.6500         
   Detection Prevalence : 0.8500         
      Balanced Accuracy : 0.6310         
                                         
       'Positive' Class : 1              
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/67936497

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档