【问题标题】:how to pass a tibble to caret::confusionmatrix()?如何将 tibble 传递给 caret::confusionmatrix()?
【发布时间】:2018-11-16 08:45:30
【问题描述】:

考虑这个简单的例子:

data_frame(truth = c(1,1,0,0),
           prediction = c(1,0,1,0),
           n_obs = c(100,10,90,50))
# A tibble: 4 x 3
  truth prediction n_obs
  <dbl>      <dbl> <dbl>
1     1          1   100
2     1          0    10
3     0          1    90
4     0          0    50

我想将这个tibble 传递给caret::confusionMatrix,这样我就可以同时获得我需要的所有指标(accuracy、recall 等)。

如您所见,tibble 包含计算性能统计信息所需的所有信息。例如,您可以看到在测试数据集中(此处未提供),有 100 个观察值,其中预测标签 1 与真实标签 1 匹配。然而,预测值为1 的90 观察结果实际上是误报。

我不想手动计算所有指标,我想求助于caret::confusionMatrix()

然而,事实证明这非常困难。在上面的tibble 上调用confusionMatrix(.) 不起作用。这里有什么解决办法吗?

谢谢!

【问题讨论】:

  • 到目前为止您尝试过什么?因为你需要其他参数才能使用caret::confusionMatrix
  • 您的表不可用。你怎么知道你有多少预测为 1 和 0?看看当你使用 'xtabs(n_obs ~ . , df)' 时会发生什么。
  • 伙计们...该表为您提供了每个类别中有多少观察值。例如,测试数据集中有 100 个观察值,其中 predicted 值 1 匹配 label 值 1
  • 本质上,这张表包含了我们需要让confusionMatrix() 工作的所有信息。但我无法这样做。也许通过事先转换成table? (插入符号接受它作为输入)。出现这个问题是因为表直接来自Spark
  • 使用你的逻辑真理和预测是完全一样的,你也有 100 和 90 的预测 1.a.k.a 什么 xtabs 显示。你的模型达到了 100% 的准确率?

标签: r dplyr r-caret confusion-matrix yardstick


【解决方案1】:

您可以使用以下内容。您必须将正类设置为 1,否则 0 将被视为正类。

confusionMatrix(xtabs(n_obs ~ prediction + truth , df), positive = "1")

Confusion Matrix and Statistics

          truth
prediction   0   1
         0  50  10
         1  90 100

               Accuracy : 0.6             
                 95% CI : (0.5364, 0.6612)
    No Information Rate : 0.56            
    P-Value [Acc > NIR] : 0.1128          

                  Kappa : 0.247           
 Mcnemar's Test P-Value : 2.789e-15       

            Sensitivity : 0.9091          
            Specificity : 0.3571          
         Pos Pred Value : 0.5263          
         Neg Pred Value : 0.8333          
             Prevalence : 0.4400          
         Detection Rate : 0.4000          
   Detection Prevalence : 0.7600          
      Balanced Accuracy : 0.6331          

       'Positive' Class : 1    

【讨论】:

  • 非常高效。 xtabs这里是救生员
  • @ℕʘʘḆḽḘ,是的,基本函数仍然有用:-)
猜你喜欢
  • 2014-05-23
  • 2021-05-21
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-12-26
  • 2020-05-07
相关资源
最近更新 更多