簡體   English   中英

如何通過小標題插入插入符號:: confusionmatrix()?

[英]how to pass a tibble to caret::confusionmatrix()?

考慮以下簡單示例:

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以便同時獲得我需要的所有指標( accuracyrecall等)。

如您所見, tibble包含計算性能統計信息所需的所有信息。 例如,您可以看到在測試數據集中(此處不可用),有100個觀察值,其中預測標簽1與真實標簽1相匹配。 但是,預測值為1 90觀察實際上是假陽性。

我不想手動計算所有指標,而是想使用caret::confusionMatrix()

然而,事實證明這是非常困難的。 在上面的tibble上調用confusionMatrix(.)無效。 這里有什么解決辦法嗎?

謝謝!

您可以使用以下內容。 您必須將正類設置為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    

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM