插入符-调整参数网格应具有列mtry

时间:2018-10-16 10:00:49

标签: r r-caret

我正在使用以下代码:

    mtry <- round(sqrt(18), 0)

gbmGrid <- expand.grid(
              interaction.depth = c(1, 2, 3, 4, 5, 6)
            , n.trees = seq(10, 10000, by = 100)
            , shrinkage = 0.01
            , n.minobsinnode = c(5, 10, 20, 30)
            , distribution = 'gaussian'
            , method = 'gbm'
            , mtry = mtry
    )

    fitControl <- trainControl(
                method = "repeatedcv"
                , number = 2
                , repeats = 3
        )

    gbmFit1 <- train(

                     Y ~

                      X1
                    + X2

                    , data = Train

                    , trControl = fitControl
                    , tuneGrid = gbmGrid
                    , verbose = FALSE
        )

但得到:

The tuning parameter grid should have columns mtry

我安装了最新的软件包,因为有人建议这样做,并且还尝试使用.mtry。有任何想法吗? (是的,我用谷歌搜索,然后看了一下)

2 个答案:

答案 0 :(得分:3)

我已将其恢复为基本知识(iris)。这行得通-gbm不存在的mtry就是问题所在:

library(datasets)
library(gbm)
library(caret)

grid <- expand.grid(
                n.trees = seq(10, 1000, by = 100)
            , interaction.depth = c(4)
            , shrinkage = c(0.01, 0.1)
            , n.minobsinnode = c(5, 10, 20, 30)        
    )

train_control <- trainControl(
                    method = "repeatedcv"
                    , number = 10
                    , repeats = 10
    )

model <- train(Petal.Width ~ Petal.Length
                        , method = 'gbm'
                        , distribution = 'gaussian'
                        , data = iris
                        , trControl = train_control
                        , tuneGrid = grid
                        , verbose = FALSE
    )

model

很抱歉浪费您的时间!

答案 1 :(得分:0)

caret的版本> = 6.0-81中,这种情况的错误消息更加清晰。例如,考虑当mtry不是给定方法的参数时,在调整网格中提供mtry

caret <6.0-81中,将发生以下错误:

# Error: The tuning parameter grid should have columns mtry

caret> = 6.0-81中,将发生以下错误:

# Error: The tuning parameter grid should not have columns mtry

代表原始令人困惑的错误消息

这是一个可复制的示例,展示了改进的错误消息。

插入符<6.0-81

library(caret)
getNamespaceVersion("caret")
## version 
## "6.0-80"

mtry <- round(sqrt(18), 0)
gbmGrid <- expand.grid(
    interaction.depth = c(1, 2, 3, 4, 5, 6)
    , n.trees = seq(10, 10000, by = 100)
    , shrinkage = 0.01
    , n.minobsinnode = c(5, 10, 20, 30)
    , distribution = 'gaussian'
    , method = 'gbm'
    , mtry = mtry
)
fitControl <- trainControl(
    method = "repeatedcv"
    , number = 2
    , repeats = 3
)
gbmFit1 <- train(
    Species ~ Sepal.Length + Sepal.Width
    , data = iris
    , trControl = fitControl
    , tuneGrid = gbmGrid
    , verbose = FALSE
)
# Error: The tuning parameter grid should have columns mtry

插入符> = 6.0-81

library(caret)
getNamespaceVersion("caret")
## version 
## "6.0-81"

mtry <- round(sqrt(18), 0)
gbmGrid <- expand.grid(
    interaction.depth = c(1, 2, 3, 4, 5, 6)
    , n.trees = seq(10, 10000, by = 100)
    , shrinkage = 0.01
    , n.minobsinnode = c(5, 10, 20, 30)
    , distribution = 'gaussian'
    , method = 'gbm'
    , mtry = mtry
)
fitControl <- trainControl(
    method = "repeatedcv"
    , number = 2
    , repeats = 3
)
gbmFit1 <- train(
    Species ~ Sepal.Length + Sepal.Width
    , data = iris
    , trControl = fitControl
    , tuneGrid = gbmGrid
    , verbose = FALSE
)
# Error: The tuning parameter grid should not have columns mtry

有关更多信息,请参阅描述的GitHub问题,然后解决此问题:https://github.com/topepo/caret/issues/955