2017-03-17 160 views
2

我一直在探索與泰坦尼克號data set的奇妙mlr包。我的問題是實施一個隨機森林。更具體地說,我想調整cutoff(即給給定類別分配不純的葉子的閾值)。問題是cutoff參數有兩個值,但是,我只能找出超參數在mlr中爲一個值。隨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

對於那些有類似的問題,更好的辦法是使用' makeNumericParam(「cutoff」,lower = .2,upper = .8,trafo = function(x)c(x,1-x))'而不是'makeDiscreteParam(「cutoff」,values = list(a = c(。 50,.50),b = c(.75,.25))'。爲了獲得詳盡的搜索,更少的編碼。 –

回答

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