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

Keeping one parameter fixed and search on randomly in caret

我想将参数 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"))

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

但是,作为替代方案,您可以使用网格搜索来优化调整参数,而不是随机搜索。然后,您可以使用 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.

我不认为这可以通过直接在插入符号中指定来实现 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)))