當前位置: 首頁>>編程示例 >>用法及示例精選 >>正文


R yardstick mn_log_loss 多項數據的平均對數損失

計算分類模型的對數損失。

用法

mn_log_loss(data, ...)

# S3 method for data.frame
mn_log_loss(
  data,
  truth,
  ...,
  na_rm = TRUE,
  sum = FALSE,
  event_level = yardstick_event_level(),
  case_weights = NULL
)

mn_log_loss_vec(
  truth,
  estimate,
  na_rm = TRUE,
  sum = FALSE,
  event_level = yardstick_event_level(),
  case_weights = NULL,
  ...
)

參數

data

包含 truth... 指定的列的 data.frame

...

一組不帶引號的列名稱或一個或多個 dplyr 選擇器函數,用於選擇哪些變量包含類概率。如果 truth 是二進製,則僅應選擇 1 列,並且它應對應於 event_level 的值。否則,列的數量應與 truth 的因子級別一樣多,並且列的順序應與 truth 的因子級別相同。

truth

真實類結果的列標識符(即 factor )。這應該是一個不帶引號的列名,盡管此參數是通過表達式傳遞的並且支持quasiquotation(您可以不帶引號的列名)。對於 _vec() 函數,一個 factor 向量。

na_rm

logical 值,指示在計算繼續之前是否應剝離 NA 值。

sum

一個logical。是否應該返回似然貢獻的總和(而不是平均值)?

event_level

單個字符串。 "first""second" 指定將truth 的哪個級別視為"event"。此參數僅適用於 estimator = "binary" 。默認使用內部幫助程序,通常默認為 "first" ,但是,如果設置了已棄用的全局選項 yardstick.event_first ,則將使用該幫助程序並發出警告。

case_weights

案例權重的可選列標識符。這應該是一個不帶引號的列名稱,其計算結果為 data 中的數字列。對於 _vec() 函數,一個數值向量。

estimate

如果truth是二進製的,對應於 "relevant" 類的類概率的數值向量。否則,矩陣的列數與因子級別一樣多truth.假設它們的順序與 truth 的級別相同。

tibble 包含列 .metric.estimator.estimate 以及 1 行值。

對於分組 DataFrame ,返回的行數將與組數相同。

對於 mn_log_loss_vec() ,單個 numeric 值(或 NA )。

細節

對數損失是分類模型性能的衡量標準。完美模型的對數損失為 0

accuracy() 相比,對數損失考慮了預測的不確定性,並更詳細地了解實際性能。例如,給定 .6.9 兩個輸入概率,其中兩者都被分類為預測正值,例如 "Yes" ,準確度度量會將它們解釋為具有相同的值。如果真實輸出是 "Yes" ,則對數損失會懲罰 .6 ,因為與 .9 的概率相比,它的結果是 "less sure" 。

多級

對數損失具有已知的多類擴展,並且隻是每個類預測的對數損失值的總和。因此,不支持任何平均類型。

也可以看看

其他類概率指標:average_precision() , brier_class() , classification_cost() , gain_capture() , pr_auc() , roc_auc() , roc_aunp() , roc_aunu()

作者

馬克斯·庫恩

例子

# Two class
data("two_class_example")
mn_log_loss(two_class_example, truth, Class1)
#> # A tibble: 1 × 3
#>   .metric     .estimator .estimate
#>   <chr>       <chr>          <dbl>
#> 1 mn_log_loss binary         0.328

# Multiclass
library(dplyr)
data(hpc_cv)

# You can use the col1:colN tidyselect syntax
hpc_cv %>%
  filter(Resample == "Fold01") %>%
  mn_log_loss(obs, VF:L)
#> # A tibble: 1 × 3
#>   .metric     .estimator .estimate
#>   <chr>       <chr>          <dbl>
#> 1 mn_log_loss multiclass     0.734

# Groups are respected
hpc_cv %>%
  group_by(Resample) %>%
  mn_log_loss(obs, VF:L)
#> # A tibble: 10 × 4
#>    Resample .metric     .estimator .estimate
#>    <chr>    <chr>       <chr>          <dbl>
#>  1 Fold01   mn_log_loss multiclass     0.734
#>  2 Fold02   mn_log_loss multiclass     0.808
#>  3 Fold03   mn_log_loss multiclass     0.705
#>  4 Fold04   mn_log_loss multiclass     0.747
#>  5 Fold05   mn_log_loss multiclass     0.799
#>  6 Fold06   mn_log_loss multiclass     0.766
#>  7 Fold07   mn_log_loss multiclass     0.927
#>  8 Fold08   mn_log_loss multiclass     0.855
#>  9 Fold09   mn_log_loss multiclass     0.861
#> 10 Fold10   mn_log_loss multiclass     0.821


# Vector version
# Supply a matrix of class probabilities
fold1 <- hpc_cv %>%
  filter(Resample == "Fold01")

mn_log_loss_vec(
  truth = fold1$obs,
  matrix(
    c(fold1$VF, fold1$F, fold1$M, fold1$L),
    ncol = 4
  )
)
#> [1] 0.7338423

# Supply `...` with quasiquotation
prob_cols <- levels(two_class_example$truth)
mn_log_loss(two_class_example, truth, Class1)
#> # A tibble: 1 × 3
#>   .metric     .estimator .estimate
#>   <chr>       <chr>          <dbl>
#> 1 mn_log_loss binary         0.328
mn_log_loss(two_class_example, truth, !!prob_cols[1])
#> # A tibble: 1 × 3
#>   .metric     .estimator .estimate
#>   <chr>       <chr>          <dbl>
#> 1 mn_log_loss binary         0.328

相關用法


注:本文由純淨天空篩選整理自Max Kuhn等大神的英文原創作品 Mean log loss for multinomial data。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。