【问题标题】:How use predict to new data?如何对新数据使用预测?
【发布时间】:2020-02-01 02:38:28
【问题描述】:

我想使用 mlr3 包创建的模型对以前未知的新数据进行预测。我使用 AutoTuner 函数训练了模型。

我阅读了 mlr3 书的“3.4.1.4 预测”一章,但该解决方案对于我想使用全新数据的示例没有用。

library("mlr3")
library("paradox")
library("mlr3learners")
library("mlr3tuning")
library("data.table")

set.seed(1)

x1 = 1:100
x2 = 2 * x1
y = x1^2 - x2 + rnorm(100)

data = data.table(
   x1 = x1,
   x2 = x2,
   y = y
)

newdata = data.table(x1 = 101:150, x2 = 2 * 101:150)

task = TaskRegr$new("task", backend = data, target = "y")

lrn_xgb = mlr_learners$get("regr.xgboost")

ps = ParamSet$new(
   params = list(
      ParamInt$new(id = "max_depth", lower = 4, upper = 10)
   ))

at = AutoTuner$new(learner = lrn_xgb, 
                   resampling = rsmp("cv", folds = 2),
                   measures = msr("regr.rmse"), 
                   tune_ps = ps,
                   terminator = term("evals", n_evals = 1),
                   tuner = tnr("random_search"))

resampling_outer = rsmp("cv", folds = 2)

rr = resample(task = task, learner = at, resampling = resampling_outer)

at$train(task)

at$predict_newdata(task, newdata)

会话信息:

R version 3.6.1 (2019-07-05)
Platform: x86_64-w64-mingw32/x64 (64-bit)
Running under: Windows 8.1 x64 (build 9600)

Matrix products: default

attached base packages:
[1] stats     graphics  grDevices utils     datasets  methods  
[7] base     

other attached packages:
[1] mlr3learners_0.1.3 mlr3tuning_0.1.0   data.table_1.12.2 
[4] paradox_0.1.0      mlr3_0.1.3

loaded via a namespace (and not attached):
 [1] lgr_0.3.3        lattice_0.20-38  mlr3misc_0.1.4  
 [4] digest_0.6.21    crayon_1.3.4     grid_3.6.1      
 [7] R6_2.4.0         backports_1.1.4  magrittr_1.5    
[10] stringi_1.4.3    uuid_0.1-2       Matrix_1.2-17   
[13] checkmate_1.9.4  xgboost_0.90.0.2 tools_3.6.1     
[16] compiler_3.6.1   Metrics_0.1.4

【问题讨论】:

  • 我无法让您的示例与最新版本的软件包一起运行。请问可以更新吗?一般来说,您可能想使用learner$predict_newdata()
  • 我有来自 CRAN 的当前版本的软件包(我在主帖中添加了会话信息)。您建议的功能可能是解决方案,但目前还有一个问题。 mlr3book 的第 4.3 章和第 10.1 章以重采样功能结束,但在 AutoTuner 文档示例中还有一个步骤“at$train(task)”。我明白这是模型最终训练的必要条件?
  • 对不起,原来我的包版本已经过时了:)

标签: r mlr3


【解决方案1】:

您需要训练选定的学习者(正如您在 cmets 中指出的那样),然后使用 predict_newdata()

at$train(task)
at$predict_newdata(task, newdata)

【讨论】:

  • 我怀疑还缺少一步将参数从 AutoTune 传递到训练函数。我在主要问题中编辑了代码,您会注意到这两个函数中的模型参数(“eta”和“max_depth”)不同。
  • 还看到我们仍在对这个成员函数进行更改:github.com/mlr-org/mlr3/issues/360#issuecomment-538318337。因此,您现在在使用它时可能会遇到问题。
  • @nukubiho “AutoTuner”会自动对其进行超标准调整,然后使用调整中的最佳设置在任务上训练模型。 “模型参数不同”是什么意思?注意不要混淆resample()$train()predict_newdata() 调用。这里的答案是正确的。
  • @nukubiho 如果您同意我的评论,如果您可以将问题标记为“已解决”,那就太好了:)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-07-23
  • 2021-11-04
  • 2022-10-22
  • 2018-05-20
  • 2018-11-29
  • 2018-10-15
相关资源
最近更新 更多