调整Caret包中随机林的两个参数

调整Caret包中随机林的两个参数,r,machine-learning,r-caret,R,Machine Learning,R Caret,当我仅使用mtry参数作为tuingrid时,它起作用,但当我添加ntree参数时,错误变为列车中的错误。默认值(x,y,weights=w,…):调谐参数网格应具有列mtry。代码如下: require(RCurl) require(prettyR) library(caret) url <- "https://raw.githubusercontent.com/gastonstat/CreditScoring/master/CleanCreditScoring.csv" cs_data

当我仅使用
mtry
参数作为
tuingrid
时,它起作用,但当我添加
ntree
参数时,错误变为列车中的
错误。默认值(x,y,weights=w,…):调谐参数网格应具有列mtry
。代码如下:

require(RCurl)
require(prettyR)
library(caret)
url <- "https://raw.githubusercontent.com/gastonstat/CreditScoring/master/CleanCreditScoring.csv"
cs_data <- getURL(url)
cs_data <- read.csv(textConnection(cs_data))
classes <- cs_data[, "Status"]
predictors <- cs_data[, -match(c("Status", "Seniority", "Time", "Age", "Expenses", 
    "Income", "Assets", "Debt", "Amount", "Price", "Finrat", "Savings"), colnames(cs_data))]

train_set <- createDataPartition(classes, p = 0.8, list = FALSE)
set.seed(123)

cs_data_train = cs_data[train_set, ]
cs_data_test = cs_data[-train_set, ]

# Define the tuned parameter
grid <- expand.grid(mtry = seq(4,16,4), ntree = c(700, 1000,2000) )

ctrl <- trainControl(method = "cv", number = 10, summaryFunction = twoClassSummary,classProbs = TRUE)

rf_fit <- train(Status ~ ., data = cs_data_train,
                    method = "rf",
                    preProcess = c("center", "scale"),
                    tuneGrid = grid,
                    trControl = ctrl,         
                   family= "binomial",
                   metric= "ROC" #define which metric to optimize metric='RMSE'
               )
rf_fit
require(RCurl)
需要(prettyR)
图书馆(插入符号)

url您必须使用random forest包创建一个自定义RF,然后包含您想要包含的参数

customRF <- list(type = "Classification", library = "randomForest", loop = NULL)
customRF$parameters <- data.frame(parameter = c("mtry", "ntree"), class = rep("numeric", 2), label = c("mtry", "ntree"))
customRF$grid <- function(x, y, len = NULL, search = "grid") {}
customRF$fit <- function(x, y, wts, param, lev, last, weights, classProbs, ...) {
    randomForest(x, y, mtry = param$mtry, ntree=param$ntree, ...)
}
customRF$predict <- function(modelFit, newdata, preProc = NULL, submodels = NULL)
    predict(modelFit, newdata)
customRF$prob <- function(modelFit, newdata, preProc = NULL, submodels = NULL)
    predict(modelFit, newdata, type = "prob")
customRF$sort <- function(x) x[order(x[,1]),]
customRF$levels <- function(x) x$classes
customRF
customRF您应该更改:

grid <- expand.grid(.mtry = seq(4,16,4),. ntree = c(700, 1000,2000) )

非常感谢much@ChirayuChamoli