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


R yardstick classification_cost 不良分類的成本函數

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

相關用法


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