调整Caret包中随机森林的两个参数 [英] Tuning two parameters for random forest in Caret package

查看:1120
本文介绍了调整Caret包中随机森林的两个参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我仅将mtry参数用作tuingrid时,它起作用了,但是当我添加了ntree参数时,错误变为Error in train.default(x, y, weights = w, ...): The tuning parameter grid should have columns mtry.代码如下:

When i only used mtry parameter as the tuingrid, it worked but when i added ntree parameter the error becomes Error in train.default(x, y, weights = w, ...): The tuning parameter grid should have columns mtry. The code is as below:

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

推荐答案

您必须使用随机森林包创建自定义RF,然后包含要包含的参数.

You have to create a custom RF using the random forest package and then include the param that you want to include.

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].

Then you can use method as [customRF] in the train function.

这篇关于调整Caret包中随机森林的两个参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆