简体   繁体   English

Ensemble模型错误以及R中的调整

[英]Error with Ensemble models and tuning in R

I've been trying to use an Ensemble method in R and was trying out the ensembles of caret models on the BostonHousing2 dataset. 我一直在尝试在R中使用Ensemble方法,并尝试在BostonHousing2数据集中尝试插入符号模型的集成 While setting up the greedy ensemble as well as the linear ensemble, I get an error. 在设置贪婪合奏和线性合奏时,出现错误。 The code is as follows: 代码如下:

library(caret)
library(caretEnsemble)
library(mlbench)
data(BostonHousing2)
X <- model.matrix(cmedv~crim+zn+indus+chas+nox+rm+age+dis+
                rad+tax+ptratio+b+lstat+lat+lon, BostonHousing2)[,-1]
X <- data.frame(X)
Y <- BostonHousing2$cmedv
train <- runif(nrow(X)) <= 0.7
folds=5
repeats=1
myControl <- trainControl(method='cv', number=folds, repeats=repeats, returnResamp='none', 
                      returnData=FALSE, savePredictions=TRUE, 
                      verboseIter=TRUE, allowParallel=TRUE,
                      index=createMultiFolds(Y[train], k=folds, times=repeats))
PP <- c('center', 'scale')
model1 <- train(X[train,], Y[train], method='gbm', trControl=myControl,
            tuneGrid=expand.grid(.n.trees=500, .interaction.depth=15, .shrinkage = 0.01))
model2 <- train(X[train,], Y[train], method='blackboost', trControl=myControl)
model3 <- train(X[train,], Y[train], method='parRF', trControl=myControl)
model4 <- train(X[train,], Y[train], method='mlpWeightDecay', trControl=myControl, trace=FALSE, preProcess=PP)
model5 <- train(X[train,], Y[train], method='ppr', trControl=myControl, preProcess=PP)
model6 <- train(X[train,], Y[train], method='earth', trControl=myControl, preProcess=PP)
model7 <- train(X[train,], Y[train], method='glm', trControl=myControl, preProcess=PP)
model8 <- train(X[train,], Y[train], method='svmRadial', trControl=myControl, preProcess=PP)
model9 <- train(X[train,], Y[train], method='gam', trControl=myControl, preProcess=PP)
model10 <- train(X[train,], Y[train], method='glmnet', trControl=myControl, preProcess=PP)
all.models <- list(model1, model2, model3, model4, model5, model6, model7, model8, model9, model10)
names(all.models) <- sapply(all.models, function(x) x$method)
sort(sapply(all.models, function(x) min(x$results$RMSE)))
greedy <- caretEnsemble(all.models, iter=1000L)
Error: is(list_of_models, "caretList") is not TRUE

There are a couple of instances where I am stuck: while setting up model1, I get the following error message: 有几个实例会卡住:设置模型1时,出现以下错误消息:

The tuning parameter grid should have columns n.trees, interaction.depth, shrinkage, n.minobsinnode

Also while setting up the greedy ensemble as well as the linear ensemble, I get a list error when combining the models. 同样在设置贪婪合奏和线性合奏时,组合模型时出现列表错误。 Request some assistance please. 请寻求帮助。

PS: Apologize, if these needed to be separate questions. PS:如果这些问题需要单独回答,请道歉。

You must use the function caretlist() in order to create an a caret-model-list which can be passed to caretEnsemble , as seen in the vignette example below: 您必须使用caretlist()函数来创建一个caret-model-list,该列表可以传递给caretEnsemble ,如以下插图插图所示:

model_list <- caretList(
  Class~., data=training,
  trControl=my_control,
  methodList=c("glm", "rpart")
  )

Answering your second question, what do you expect the following line to do? 回答您的第二个问题,您期望下一行做什么?

expand.grid(.n.trees=500, .interaction.depth=15, .shrinkage = 0.01)

You can test that it is just a single combination of three values. 您可以测试它只是三个值的单一组合。 You should have atleast two values in any of the columns to generate more than 1 parameter value combinations to tune on. 您应该在任何列中至少包含两个值,以生成多个要调谐的参数值组合。 Also , the why do the names have an additional "." 另外 ,为什么名称中还有一个附加的“。” (dot) at the beginning? (点)开头?

Answering the second part: 回答第二部分:

The updated version of method="gbm" requires presence of n.trees, interaction.depth, shrinkage, n.minobsinnode in tuneGrid. method =“ gbm”的更新版本要求tuneGrid中存在n.trees,interaction.depth,收缩,n.minobsinnode。

tuneGrid=expand.grid(.n.trees=500, .interaction.depth=6, .shrinkage = 0.01,.n.minobsinnode = c(10))

would work just fine in the case above. 在上述情况下将可以正常工作。

It works if we convert the 'all.models' which is a normal list to a Caret_List using the code - 如果我们使用以下代码将正常列表“ all.models”转换为Caret_List,则此方法有效-

class(all.models) <- "caretList" class(all.models)<-“ caretList”

Thanks! 谢谢!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM