2

library(mlr3verse)
preformace_msr <- msr("classif.fbeta", beta = 1.5)

我正在尝试在 fbeta 度量中使用 BETA 的自定义值来调整分类模型。

但是上述试图给出 beta 值的方式在 mlr3 中会引发错误。

在 mlr3 中正确的做法是什么?

4

2 回答 2

2

所以错误如下:

library(mlr3verse)
preformace_msr <- msr("classif.fbeta", beta = 1.5)
#> Error: Cannot set argument 'beta' for 'MeasureBinaryimple' (not a constructor argument, not a parameter, not a field.

我认为这与只有 F1-Measure(不是 F1.5-measure 等)在mlr3. 见beta=1 源文件

mlr3 书中,您将在 6.3 下找到以下内容:

在本节中,我们展示了如何实现自定义性能度量。

根据本节,您可以使用以下方法实现 Fx-Measure(我认为我的语法不是最好/最安全的,但它应该可以工作):

library(mlr3verse)
library(mlr3measures)
library(R6)

# make custom measure:
MeasureCustomFbeta = R6::R6Class("classif.custom_fbeta",
                             inherit = mlr3::MeasureClassif,
                             public = list(
                               #declase field
                               beta=NULL, #delcare field
                               initialize = function(beta) {
                                 self$beta <- beta
                                 super$initialize(
                                   # custom id for the measure
                                   id = "custom_fbeta",
                                  
                                   # required predict type of the learner
                                   predict_type = "response",
                                   
                                   # feasible range of values
                                   range = c(0, Inf),
                                   
                                   # minimize during tuning?
                                   minimize = TRUE
                                 )
                               }
                             ),
  
                             
                             private = list(
                               # customized scoring function operating on the prediction object
                               .score = function(prediction, ...) {
                                 fbeta_cm = function(m, beta) {
                                   pred_pos = sum(m[1L, ])
                                   cond_pos = sum(m[, 1L])
                                   if (m[1L, 1L] == 0L || pred_pos == 0L || cond_pos == 0L)
                                     return(na_value)
                                   
                                   P = m[1L, 1L] / pred_pos
                                   R = m[1L, 1L] / cond_pos
                                   ((1 + beta^2) * P * R) / ((beta^2 * P) + R)
                                 }
                                 fbeta_cm(confusion_matrix(prediction$truth, prediction$response, prediction$positive)$matrix, self$beta)
                                 
                               })
                               
)

# add it to the dictionary
mlr3::mlr_measures$add("classif.custom_fbeta", MeasureCustomFbeta)

测试:

# get data
data("Sonar", package = "mlbench")

# make task
task = TaskClassif$new(id = "Sonar", Sonar, target = "Class", positive = "R")

# make learner
learner = lrn("classif.rpart", predict_type = "response")

# predict
pred = learner$train(task)$predict(task)
pred$confusion
#>         truth
#> response  R  M
#>        R 87 16
#>        M 10 95

# measure "classif.beta
measure_old <- msr("classif.fbeta")
pred$score(measure_old)
#> classif.fbeta 
#>          0.87

# customized measure
measure_new <- msr("classif.custom_fbeta", beta=1.5)
pred$score(measure_new)
#> classif.custom_fbeta 
#>            0.8801556
于 2021-04-03T20:22:11.977 回答
2

到目前为止,措施还没有参数。将在下一个版本中通过https://github.com/mlr-org/mlr3/pull/623解决。

于 2021-04-12T09:40:59.397 回答