代码之家  ›  专栏  ›  技术社区  ›  Robert Long

用mlr预测计数

  •  2
  • Robert Long  · 技术社区  · 7 年前

    我在用学习者 regr.gbm 预测计数。外部 mlr ,使用 gbm 直接打包,我用 distribution = "poisson" predict.gbm ,使用 type = "response" ,返回原始比例的预测,但是我注意到,当我使用 最大似然比 ,预测似乎在对数刻度上:

         truth    response
    913      4  0.67348708
    914      1  0.28413256
    915      3  0.41871237
    916      1  0.13027792
    2101     1 -0.02092168
    2102     2  0.23394970
    

    然而,“真相”并不在日志范围内,因此我担心 最大似然比 不会起作用的。作为比较,这是我得到的输出 distribution = "gaussian" .

         truth response
    913      4 2.028177
    914      1 1.334658
    915      3 1.552846
    916      1 1.153072
    2101     1 1.006362
    2102     2 1.281811
    

    处理这个问题的最好方法是什么?

    1 回复  |  直到 7 年前
        1
  •  2
  •   mb706    7 年前

    这是因为 gbm 默认情况下,对链接功能比例(即 log 对于 distribution = "poisson" ). 这是由 type 参数 gbm::predict.gbm (请参见该函数的帮助页)。不幸的是 mlr 默认情况下不提供更改此参数的功能( it was reported 在mlr bugtracker中)。目前的解决方法是手动添加此参数:

    lrn <- makeLearner("regr.gbm", distribution = "poisson")
    lrn$par.set <- c(lrn$par.set,
      makeParamSet(
        makeDiscreteLearnerParam("type", c("link", "response"),
          default = "link", when = "predict", tunable = FALSE)))
    lrn <- setHyperPars(lrn, type = "response")
    
    # show that it works:
    counttask <- makeRegrTask("counttask", getTaskData(pid.task),
      target = "pregnant")
    pred <- predict(train(lrn, counttask), counttask)
    pred
    

    请注意,在调整计数数据的参数时,默认的回归度量(平方误差的平均值)可能会过分强调适合具有较大计数值的数据点的大小。预测“10”而不是“1”的平方误差与预测“1010”而不是“1001”的误差相同,但根据您的目标,您可能希望在本例中更重视第一个误差。

    一种可能的解决方法是使用(标准化的)平均泊松对数似然作为度量:

    poisllmeasure = makeMeasure(
      id = "poissonllnorm",
      minimize = FALSE,
      best = 0,
      worst = -Inf,
      properties = "regr",
      name = "Mean Poisson Log Likelihood",
      note = "For count data. Normalized to 0 for perfect fit.",
      fun = function(task, model, pred, feats, extra.args) {
        mean(dpois(pred$data$truth, pred$data$response, log = TRUE) -
          dpois(pred$data$truth, pred$data$truth, log = TRUE))
    })
    # example
    performance(pred, poisllmeasure)
    

    此度量值可用于调整,方法是 measures 参数输入 tuneParams() . (请注意,您必须在列表中列出: tuneParams(... measures = list(poisllmeasure) ...) )

    推荐文章