我试图使用xgboost对虹膜数据进行分类,但遇到此错误。
“ frankv中的错误(预测):x是一个列表,'cols'不能为0长度 另外:警告消息: 在train.default(x_train,y_train,trControl = ctrl,tuneGrid = xgbgrid,: 无法计算回归的类概率”
我正在使用以下代码。任何帮助或解释将不胜感激。
data(iris)
library(caret)
library(dplyr)
library(xgboost)
set.seed(123)
index <- createDataPartition(iris$Species, p=0.8, list = FALSE)
trainData <- iris[index,]
testData <- iris[-index,]
x_train = xgb.DMatrix(as.matrix(trainData %>% select(-Species)))
y_train = as.numeric(trainData$Species)
#### Generic control parametrs
ctrl <- trainControl(method="repeatedcv",
number=10,
repeats=5,
savePredictions=TRUE,
classProbs=TRUE,
summaryFunction = twoClassSummary)
xgbgrid <- expand.grid(nrounds = 10,
max_depth = 5,
eta = 0.05,
gamma = 0.01,
colsample_bytree = 0.75,
min_child_weight = 0,
subsample = 0.5,
objective = "binary:logitraw",
eval_metric = "error")
set.seed(123)
xgb_model = train(x_train,
y_train,
trControl = ctrl,
tuneGrid = xgbgrid,
method = "xgbTree")
答案 0 :(得分:0)
有几个问题:
结果变量应该是一个因素。
调谐网格具有插入符号的调谐网格未使用的参数。
由于存在三个级别,因此使用两个类的摘要将是不合适的。多类摘要与summaryFunction = multiClassSummary
一起使用。
一个工作示例:
data(iris)
library(caret)
library(dplyr)
library(xgboost)
set.seed(123)
index <- createDataPartition(iris$Species, p=0.8, list = FALSE)
trainData <- iris[index,]
testData <- iris[-index,]
x_train = xgb.DMatrix(as.matrix(trainData %>% select(-Species)))
y_train = as.factor(trainData$Species)
#### Generic control parametrs
ctrl <- trainControl(method="repeatedcv",
number=10,
repeats=5,
savePredictions=TRUE,
classProbs=TRUE,
summaryFunction = multiClassSummary)
xgbgrid <- expand.grid(nrounds = 10,
max_depth = 5,
eta = 0.05,
gamma = 0.01,
colsample_bytree = 0.75,
min_child_weight = 0,
subsample = 0.5)
set.seed(123)
x_train
xgb_model = train(x_train,
y_train,
trControl = ctrl,
method = "xgbTree",
tuneGrid = xgbgrid)
xgb_model