Tidymodels class 成本

Tidymodels class cost

我正在处理一个预测案例,其中数据在二进制预测目标中存在严重的不平衡。有没有一种方法可以用 TidyModels 中的成本矩阵来惩罚少数 class 的错误预测?我知道 caret 实现了这个,但我在 TidyModels 中找到的信息非常混乱。 我所找到的只是来自实验法棍包的 baguette::class_cost() 函数,它似乎只适用于袋装树模型。

是的,你想设置一个classification_cost():

library(yardstick)
#> For binary classification, the first factor level is assumed to be the event.
#> Use the argument `event_level = "second"` to alter this as needed.
library(dplyr)
#> 
#> Attaching package: 'dplyr'
#> The following objects are masked from 'package:stats':
#> 
#>     filter, lag
#> The following objects are masked from 'package:base':
#> 
#>     intersect, setdiff, setequal, union
# 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

reprex package (v2.0.1)

于 2021-10-27 创建

在 tidymodels 中,您可以使用此指标来计算事后结果或调整结果。学习 more here.