2017-07-20 71 views
2

我正在嘗試使用MLR軟件包來調整使用rpart軟件包構建的決策樹的超參數。即使我可以調整決策樹的基本參數(例如minsplit,maxdepth等),我也無法正確設置參數param的值。具體來說,我想在網格搜索中嘗試不同的priors在MLR包中調整rpart中的parms?

在這裏,我寫的代碼(dat是我使用的數據幀,並target是我的類變量):

# Create a task 
dat.task = makeClassifTask(id = "tree", data = dat, target = "target") 
# Define the model 
resamp = makeResampleDesc("CV", iters = 4L) 
# Create the learner 
lrn = makeLearner("classif.rpart") 
# Create the grid params 
control.grid = makeTuneControlGrid() 
ps = makeParamSet(
    makeDiscreteParam("cp", values = seq(0.001, 0.006, 0.002)), 
    makeDiscreteParam("minsplit", values = c(1, 5, 10, 50)), 
    makeDiscreteParam("maxdepth", values = c(20, 30, 50)), 
    makeDiscreteParam("parms", values = list(prior=list(c(.6, .4), 
                 c(.5, .5)))) 
) 

當我試圖執行的微調,加上:

# Actual tuning, with accuracy as evaluation metric 
tuned = tuneParams(lrn, task = dat.task, 
        resampling = resamp, 
        control = control.grid, 
        par.set = ps, measures = acc) 

我收到錯誤

Error in get(paste("rpart", method, sep = "."), envir = environment())(Y, : The parms list must have names

我也試圖定義parmsUntypedParam

makeUntypedParam("parms", special.vals = list(prior=list(c(.6, .4), c(.5,.5)))) 

這是因爲,通過鍵入getParamSet("classif.rpart"),在我看來,調諧接受一個「無類型變量」,而不是一個離散的一。

然而,當我嘗試,我得到的錯誤:

Error in makeOptPath(par.set, y.names, minimize, add.transformed.x, include.error.message, : 
    OptPath can currently only be used for: numeric,integer,numericvector,integervector,logical,logicalvector,discrete,discretevector,character,charactervector 

有人能幫忙嗎?

回答

2

你必須定義參數"parms"這樣的:

makeDiscreteParam("parms", values = list(a = list(prior = c(.6, .4)), b = list(prior = c(.5, .5)))) 

ab可以是任意的名字,只是反映實際價值的話。

+0

完美,謝謝! – aprospero