保持一个参数固定并在插入符中随机搜索

时间:2017-12-13 09:30:25

标签: r r-caret glmnet lasso

我想将参数alpha固定为1并使用随机搜索lambda,这可能吗?

library(caret)

X <- iris[, 1:4]
Y <- iris[, 5]

fit_glmnet <- train(X, Y, method = "glmnet", tuneLength = 2, trControl = trainControl(search = "random"))

2 个答案:

答案 0 :(得分:3)

我不认为这可以通过直接在插入符train中指定来实现,但这里是如何模拟所需的行为:

从此link

可以看到随机搜索lambda是通过以下方式实现的:

lambda = 2^runif(len, min = -10, 3)

其中len是曲调长度

模仿一个参数的随机搜索:

len <- 2
fit_glmnet <- train(X, Y,
                    method = "glmnet",
                    tuneLength = len,
                    trControl = trainControl(search = "grid"),
                    tuneGrid = data.frame(alpha = 1, lambda = 2^runif(len, min = -10, 3)))

答案 1 :(得分:2)

首先,我不确定您是否可以使用随机搜索修复特定的调整参数。

但是,作为替代方案,您可以使用网格搜索来优化调整参数,而不是随机搜索。然后,您可以使用tuneGrid

修复调整参数
fit <- train(
    X,
    Y,
    method = "glmnet",
    tuneLength = 2,
    trControl = trainControl(search = "grid"),
    tuneGrid = data.frame(alpha = 1, lambda = 10^seq(-4, -1, by = 0.5)));
 fit;
 #glmnet
 #
 #150 samples
 #  4 predictor
 #  3 classes: 'setosa', 'versicolor', 'virginica'
 #
 #No pre-processing
 #Resampling: Bootstrapped (25 reps)
 #Summary of sample sizes: 150, 150, 150, 150, 150, 150, ...
 #Resampling results across tuning parameters:
 #
 #  lambda        Accuracy   Kappa
 #  0.0001000000  0.9398036  0.9093246
 #  0.0003162278  0.9560817  0.9336278
 #  0.0010000000  0.9581838  0.9368050
 #  0.0031622777  0.9589165  0.9379580
 #  0.0100000000  0.9528997  0.9288533
 #  0.0316227766  0.9477923  0.9212374
 #  0.1000000000  0.9141015  0.8709753
 #
 #Tuning parameter 'alpha' was held constant at a value of 1
 #Accuracy was used to select the optimal model using  the largest value.
 #The final values used for the model were alpha = 1 and lambda = 0.003162278.