1

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

2 回答 2

4

我认为这不能通过直接在插入符号中指定来实现,train但这是模拟所需行为的方法:

从这个链接

可以看到对 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)))
于 2017-12-13T11:15:30.590 回答
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.
于 2017-12-13T10:34:46.933 回答