計算分類模型的對數損失。
用法
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
相關用法
- R yardstick mae 平均絕對誤差
- R yardstick msd 平均符號偏差
- R yardstick mpe 平均百分比誤差
- R yardstick metrics 估計性能的通用函數
- R yardstick metric_set 組合度量函數
- R yardstick mape 平均絕對百分比誤差
- R yardstick metric_tweak 調整度量函數
- R yardstick mcc 馬修斯相關係數
- R yardstick mase 平均絕對比例誤差
- R yardstick pr_auc 查準率曲線下麵積
- R yardstick accuracy 準確性
- R yardstick gain_capture 增益捕獲
- R yardstick pr_curve 精確率召回曲線
- R yardstick conf_mat 分類數據的混淆矩陣
- R yardstick rpd 性能與偏差之比
- R yardstick detection_prevalence 檢測率
- R yardstick bal_accuracy 平衡的精度
- R yardstick rpiq 績效與四分位間的比率
- R yardstick roc_aunp 使用先驗類別分布,每個類別相對於其他類別的 ROC 曲線下麵積
- R yardstick roc_curve 接收者算子曲線
- R yardstick rsq R 平方
- R yardstick iic 相關性理想指數
- R yardstick recall 記起
- R yardstick roc_aunu 使用均勻類別分布,每個類別相對於其他類別的 ROC 曲線下麵積
- R yardstick npv 陰性預測值
注:本文由純淨天空篩選整理自Max Kuhn等大神的英文原創作品 Mean log loss for multinomial data。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。