r-运行Elastic Net Regression Model后,当试图获取lambda和alpha值时,输出滞后



我是R和弹性网络回归模型的新手。我在默认数据集titanic上运行Elastic Net回归模型。运行train函数后,我正在尝试获取Alpha和Lambda值。然而,当我运行train函数时,输出一直滞后,我不得不等待输出,但根本没有输出。它是空的。。。。我正在尝试调整参数。

data(Titanic)
example<- as.data.frame(Titanic)
example['Country'] <- NA
countryunique <- array(c("Africa","USA","Japan","Australia","Sweden","UK","France"))
new_country <- c()
#Perform looping through the column, TLD
for(loopitem in example$Country)
{
#Perform random selection of an array, countryunique 
loopitem <- sample(countryunique, 1)
#Load the new value to the vector
new_country<- c(new_country,loopitem)
}
#Override the Country column with new data
example$Country<- new_country
example$Class<- as.factor(example$Class)
example$Sex<- as.factor(example$Sex)
example$Age<- as.factor(example$Age)
example$Survived<- as.factor(example$Survived)
example$Country<- as.factor(example$Country)
example$Freq<- as.numeric(example$Freq)
set.seed(12345678)
trainRowNum <- createDataPartition(example$Survived, #The outcome variable
#proportion of example to form the training set
p=0.3,
#Don't store the result in a list
list=FALSE);
# Step 2: Create the training mydataset
trainData <- example[trainRowNum,]
# Step 3: Create the test mydataset
testData <- example[-trainRowNum,]
alphas <- seq(0.1,0.9,by=0.1);
lambdas <- 10^seq(-3,3,length=100) 
#Logistic Elastic-Net Regression
en <- train(Survived~. , 
data = trainData, 
method = "glmnet", 
preProcess = NULL,
trControl = trainControl("repeatedcv",
number = 10,
repeats = 5),
tuneGrid = expand.grid(alpha = alphas,
lambda = lambdas)
)

你能告诉我建议给Alpha和lambda分配什么值吗?谢谢

我不太确定问题出在哪里。你的代码对我来说运行得很好。如果我查看en对象,它会显示:

Accuracy was used to select the optimal model using the
largest value.
The final values used for the model were alpha = 0.1 and lambda
= 0.1.

我没花多长时间就运行了。你的R会话内存中存储了很多可能会减慢系统速度并导致其滞后的东西吗?也许可以尝试重新启动RStudio并从头开始运行上面的代码。

要查看Alpha和Lambda所有组合的准确度完整结果表,请查看en$results

附带说明一下,您可以使用cv.glmnet函数直接在glmnet包中轻松地执行交叉验证。还提供了一个名为glmnetUtils的帮助程序包,使您可以使用cva.glmnet函数同时选择最佳Alpha和Lambda值。这允许并行化,因此可能比通过caret进行交叉验证更快。

最新更新