【问题标题】:Tidymodels class costTidymodels 课程费用
【发布时间】:2021-10-27 13:15:49
【问题描述】:

我正在处理一个预测案例,其中数据在二元预测目标中存在严重不平衡。有没有办法用 TidyModels 中的成本矩阵来惩罚少数类的错误预测?我知道插入符号已经实现了这个,但是我在 TidyModels 中找到的信息非常混乱。 我发现的只是实验 baguette 包中的 baguette::class_cost() 函数,它似乎只适用于袋装树模型。

【问题讨论】:

标签: r tidymodels imbalanced-data


【解决方案1】:

是的,你想设置一个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

【讨论】:

  • 谢谢你给我看这个,我自己没有找到这个。如果我可以提出一个后续问题:我将如何使用此指标进行调整?只需将classification_cost 函数及其成本矩阵用于tune_grid() 中的metrics 参数?到目前为止,我还没有使用带有 metrics-argument 参数的函数。
  • 您创建一个metric_set(),然后在调整函数中使用它。我有几篇博客文章证明了这一点,such as this one
猜你喜欢
  • 1970-01-01
  • 2021-09-25
  • 2020-03-10
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-09-16
相关资源
最近更新 更多