我正在处理一个预测案例,其中数据在二进制预测目标中存在强烈的不平衡。是否有一种方法可以用TidyModels中的成本矩阵来惩罚对少数阶级的错误预测?我知道插入符号已经实现了这一点,但是我在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包(v2.0.1)在2018-10-27创建
在tidymodels中,您可以在事后或调优中使用该度量来计算结果。点击这里了解更多信息。