caret - 调整参数网格应该有列 mtry

caret - The tuning parameter grid should have columns mtry

我正在使用这个代码:

    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。有任何想法吗? (是的,我用谷歌搜索并查看了 SO)

我已经把它带回了基础(虹膜)。这有效 - 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

抱歉浪费您的时间!

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