0

在使用 mlr3 时,我正在努力通过图书馆护林员训练随机森林。我设置了 3 个参数,但不知道如何初始化训练。有人可以帮我在 r 中调试这段代码吗?

pension_trunc<-subset(pension,select=-c(1:8,10:11,18,20,22,24:31,33:44))
names(pension_trunc)
str(pension_trunc)
#setting up the training and test sets
library(mlr3)
#
#define task
task<-TaskRegr$new(id="pension",backend=pension_trunc,target="net_tfa")
print(task)
#splitting the truncated dataset into training and test samples
trn_trunc<-sample(task$nrow,0.8*task$nrow)
test_trunc<-setdiff(seq_len(task$nrow),trn_trunc)
str(trn_trunc)
str(test_trunc)
#------------------------------------------------------------------------
#Set parameters
PS=ParamSet$new(list(
  ParamInt$new(id="mtry",default=3L,lower=1L,upper=5L,tags="train"),
  ParamInt$new(id="max.depth",default=5L,lower=1L,upper=30L,tags="train"),
  ParamInt$new(id="min.node.size",default=10L,lower=1L,upper=30L,tags="train")))
PS
#define learner for random forest using ranger library
learner8=lrn("regr.ranger")
#num.trees = 500 set as default value
#------------------------------------------------
#--------------------------------------------
#train random forest learner
lp_rf<-learner4$train(task=task,row_ids=trn_trunc,paramSet=PS)
lp_rf
#predict  rf on training sample
pp_rf=learner4$predict(task=task,row_ids=trn_trunc)
pp_rf
autoplot(pp_rf)
coef(learner4$model,newdata=trn_trunc)
#predict rf on test sample
pp_rf_test=learner4$predict(task=task,row_ids=test_trunc)
pp_rf_test
#
measure=msr("regr.mse")
prediction$score(measure)
4

0 回答 0