使用MLR包

时间:2017-03-17 23:09:59

标签: r mlr

我一直在用泰坦尼克号data set探索奇妙的mlr包裹。我的问题是实施随机森林。更具体地说,我想调整cutoff(即为给定类分配不纯的叶子的阈值)。问题是cutoff参数有两个值,但是,我只能在mlr中找出单参数的超参数。

代码:

library(mlr)
library(dplyr)

dTrain <- read.csv('path/to/data/')

#Defining the Task
trainTask <- makeClassifTask(data = dTrain %>% 
                           select(-Name, -Ticket, -Cabin) %>% 
                           filter(complete.cases(.)), 
                         target = "Survived", 
                         id = "PassengerId")

#Defining Learning
rfLRN <- makeLearner("classif.randomForest")

#Defining the Parameter Space
ps <- makeParamSet(
 makeDiscreteParam("cutoff", values = list(c(.5,.5), c(.75,.25)))
)

问题在于,cutoff需要两个值,但是,我不确定如何传递这两个值。上述尝试是错误的。我尝试了其他几个参数制作者,即makeDiscreteVectorParam等......但无济于事。有什么提示吗?

如果我尝试调整像mtry这样的参数(即在给定分割中选择的要素数量),一切正常。

#Defining the Hyperparameter Space
ps = makeParamSet(
  makeDiscreteParam("mtry", values = c(2,3,4,5))
)

#Defining Resampling
cvTask <- makeResampleDesc("CV", iters=5L)

#Defining Search
search <-  makeTuneControlGrid()

#Tune!
tune <- tuneParams(learner = rfLRN
                 ,task = trainTask
                 ,resampling = cvTask
                 ,measures = list(acc)
                 ,par.set = ps
                 ,control = search
                 ,show.info = TRUE)

1 个答案:

答案 0 :(得分:2)

看起来您需要为这些分类截止值指定名称,例如:

#Defining the Parameter Space
ps <- makeParamSet(
  makeDiscreteParam("cutoff", values = list(
    a=c(.50,.50),
    b=c(.75,.25)))
)

输出:

> tune <- tuneParams(learner = rfLRN
+                    ,task = trainTask
+                    ,resampling = cvTask
+                    ,measures = list(acc)
+                    ,par.set = ps
+                    ,control = search
+                    ,show.info = TRUE)
[Tune] Started tuning learner classif.randomForest for parameter set:
           Type len Def Constr Req Tunable Trafo
cutoff discrete   -   -    a,b   -    TRUE     -
With control class: TuneControlGrid
Imputation value: -0
[Tune-x] 1: cutoff=a
[Tune-y] 1: acc.test.mean=0.828; time: 0.0 min
[Tune-x] 2: cutoff=b
[Tune-y] 2: acc.test.mean=0.776; time: 0.0 min
[Tune] Result: cutoff=a : acc.test.mean=0.828