How to random search in a specified grid in caret package?

空扰寡人 提交于 2019-12-12 01:26:20

问题


I wonder it is possible to use random search in a predefined grid. For example, my grid has alpha and lambda for glmnet method. alpha is between 0 and 1, and lambda is between -10 to 10. I want to use random search 5 times to randomly try points in this bound. I wrote the following code for grid search and it works fine, but I cannot modify it for random search in a bound.

rand_ctrl <- trainControl(method = "repeatedcv", repeats = 5,
                          search = "random")
grid <- expand.grid(alpha=seq(0,1,0.1),lambda=seq(-10,10,1)) # I think this should be modified
rand_search <- train(Response ~ ., data = train_dat,
                     method = "glmnet",
                     ## Create 20 random parameter values
                     metric = "RMSE",
                     tuneLength = 5,
                     preProc = c("scale"),
                     tuneGrid = grid,
                     trControl = rand_ctrl)

回答1:


One approach would be to define a grid and use sample to pick several random rows:

set.seed(1)
samp <- sample(1:nrow(grid), 5)
grid[samp,]
#output
    alpha lambda
62    0.6     -5
86    0.8     -3
132   1.0      1
208   0.9      8
46    0.1     -6

and then use this subset as tuneGrid argument

Another approach would be to use runif which generates random numbers from a uniform distribution defined by lower and upper bound:

set.seed(1)
data.frame(alpha = runif(5, 0 , 1),
           lambda = runif(5, -10, 10))
#output
      alpha    lambda
1 0.2655087  7.967794
2 0.3721239  8.893505
3 0.5728534  3.215956
4 0.9082078  2.582281
5 0.2016819 -8.764275

and provide this as tuneGrid argument.

The second approach does not pick random elements from a grid but rather random numbers between defined minimum and maximum.



来源:https://stackoverflow.com/questions/53716810/how-to-random-search-in-a-specified-grid-in-caret-package

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!