classification_cost()
根據用戶定義的成本計算不良預測的成本。將成本乘以估計的類別概率並返回平均成本。
用法
classification_cost(data, ...)
# S3 method for data.frame
classification_cost(
data,
truth,
...,
costs = NULL,
na_rm = TRUE,
event_level = yardstick_event_level(),
case_weights = NULL
)
classification_cost_vec(
truth,
estimate,
costs = NULL,
na_rm = TRUE,
event_level = yardstick_event_level(),
case_weights = NULL,
...
)
參數
- data
-
包含
truth
和...
指定的列的data.frame
。 - ...
-
一組不帶引號的列名稱或一個或多個
dplyr
選擇器函數,用於選擇哪些變量包含類概率。如果truth
是二進製,則僅應選擇 1 列,並且它應對應於event_level
的值。否則,列的數量應與truth
的因子級別一樣多,並且列的順序應與truth
的因子級別相同。 - truth
-
真實類結果的列標識符(即
factor
)。這應該是一個不帶引號的列名,盡管此參數是通過表達式傳遞的並且支持quasiquotation(您可以不帶引號的列名)。對於_vec()
函數,一個factor
向量。 - costs
-
具有列
"truth"
、"estimate"
和"cost"
的 DataFrame 。"truth"
和"estimate"
應該是包含truth
因子級別的唯一組合的字符列。"costs"
應該是一個數字列,表示預測"estimate"
時應應用的成本,但真實結果是"truth"
。通常情況下,當
"truth" == "estimate"
時,成本為零(正確預測不會受到懲罰)。如果
truth
級別的任何組合丟失,則假定它們的成本為零。如果
NULL
,則使用相等的成本,將0
的成本應用於正確的預測,並將1
的成本應用於錯誤的預測。 - na_rm
-
logical
值,指示在計算繼續之前是否應剝離NA
值。 - 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 ,返回的行數將與組數相同。
對於 class_cost_vec()
,單個 numeric
值(或 NA
)。
細節
例如,假設存在三個類: "A"
、 "B"
和 "C"
。假設存在真正的 "A"
觀察,其類概率為 A = 0.3 / B = 0.3 / C = 0.4
。假設,當真實結果是類 "A"
時,每個類的成本為 A = 0 / B = 5 / C = 10
,錯誤預測 "C"
的概率比預測 "B"
受到的懲罰更大。此預測的成本為 0.3 * 0 + 0.3 * 5 + 0.4 * 10
。該計算針對每個樣本進行,並對各個成本進行平均。
也可以看看
其他類概率指標:average_precision()
, brier_class()
, gain_capture()
, mn_log_loss()
, pr_auc()
, roc_auc()
, roc_aunp()
, roc_aunu()
例子
library(dplyr)
# ---------------------------------------------------------------------------
# Two class example
data(two_class_example)
# Assuming `Class1` is our "event", this penalizes false positives heavily
costs1 <- tribble(
~truth, ~estimate, ~cost,
"Class1", "Class2", 1,
"Class2", "Class1", 2
)
# Assuming `Class1` is our "event", this penalizes false negatives heavily
costs2 <- tribble(
~truth, ~estimate, ~cost,
"Class1", "Class2", 2,
"Class2", "Class1", 1
)
classification_cost(two_class_example, truth, Class1, costs = costs1)
#> # A tibble: 1 × 3
#> .metric .estimator .estimate
#> <chr> <chr> <dbl>
#> 1 classification_cost binary 0.288
classification_cost(two_class_example, truth, Class1, costs = costs2)
#> # A tibble: 1 × 3
#> .metric .estimator .estimate
#> <chr> <chr> <dbl>
#> 1 classification_cost binary 0.260
# ---------------------------------------------------------------------------
# Multiclass
data(hpc_cv)
# Define cost matrix from Kuhn and Johnson (2013)
hpc_costs <- tribble(
~estimate, ~truth, ~cost,
"VF", "VF", 0,
"VF", "F", 1,
"VF", "M", 5,
"VF", "L", 10,
"F", "VF", 1,
"F", "F", 0,
"F", "M", 5,
"F", "L", 5,
"M", "VF", 1,
"M", "F", 1,
"M", "M", 0,
"M", "L", 1,
"L", "VF", 1,
"L", "F", 1,
"L", "M", 1,
"L", "L", 0
)
# You can use the col1:colN tidyselect syntax
hpc_cv %>%
filter(Resample == "Fold01") %>%
classification_cost(obs, VF:L, costs = hpc_costs)
#> # A tibble: 1 × 3
#> .metric .estimator .estimate
#> <chr> <chr> <dbl>
#> 1 classification_cost multiclass 0.779
# Groups are respected
hpc_cv %>%
group_by(Resample) %>%
classification_cost(obs, VF:L, costs = hpc_costs)
#> # A tibble: 10 × 4
#> Resample .metric .estimator .estimate
#> <chr> <chr> <chr> <dbl>
#> 1 Fold01 classification_cost multiclass 0.779
#> 2 Fold02 classification_cost multiclass 0.735
#> 3 Fold03 classification_cost multiclass 0.654
#> 4 Fold04 classification_cost multiclass 0.754
#> 5 Fold05 classification_cost multiclass 0.777
#> 6 Fold06 classification_cost multiclass 0.737
#> 7 Fold07 classification_cost multiclass 0.743
#> 8 Fold08 classification_cost multiclass 0.749
#> 9 Fold09 classification_cost multiclass 0.760
#> 10 Fold10 classification_cost multiclass 0.771
相關用法
- R yardstick conf_mat 分類數據的混淆矩陣
- R yardstick ccc 一致性相關係數
- R yardstick pr_auc 查準率曲線下麵積
- R yardstick accuracy 準確性
- R yardstick gain_capture 增益捕獲
- R yardstick pr_curve 精確率召回曲線
- R yardstick mn_log_loss 多項數據的平均對數損失
- R yardstick rpd 性能與偏差之比
- R yardstick mae 平均絕對誤差
- 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 msd 平均符號偏差
- R yardstick mpe 平均百分比誤差
- R yardstick iic 相關性理想指數
- R yardstick recall 記起
- R yardstick roc_aunu 使用均勻類別分布,每個類別相對於其他類別的 ROC 曲線下麵積
- R yardstick npv 陰性預測值
- R yardstick rmse 均方根誤差
- R yardstick sens 靈敏度
- R yardstick rsq_trad R 平方 - 傳統
- R yardstick poisson_log_loss 泊鬆數據的平均對數損失
注:本文由純淨天空篩選整理自Max Kuhn等大神的英文原創作品 Costs function for poor classification。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。