我正在尝试调整xgboost的Hyperparametes用于分类问题,使用插入库,因为我的数据集中有很多因素,xgboost喜欢数据作为数字,我使用Feature Hashing创建了一个虚拟行,但是当我得到运行插入火车,我收到错误
#Using Feature hashing to convert all the factor variables to dummies
objTrain_hashed = hashed.model.matrix(~., data=train1[,-27], hash.size=2^15, transpose=FALSE)
#created a dense matrix which is normally accepted by xgboost method in R
#Hoping I could pass it caret as well
dmodel <- xgb.DMatrix(objTrain_hashed[, ], label = train1$Walc)
xgb_grid_1 = expand.grid(
nrounds = 500,
max_depth = c(5, 10, 15),
eta = c(0.01, 0.001, 0.0001),
gamma = c(1, 2, 3),
colsample_bytree = c(0.4, 0.7, 1.0),
min_child_weight = c(0.5, 1, 1.5)
)
xgb_trcontrol_1 = trainControl(
method = "cv",
number = 3,
verboseIter = TRUE,
returnData = FALSE,
returnResamp = "all", # save losses across all models
classProbs = TRUE, # set to TRUE for AUC to be computed
summaryFunction = twoClassSummary,
allowParallel = TRUE
)
xgb_train1 <- train(Walc ~.,dmodel,method = 'xgbTree',trControl = xgb_trcontrol_1,
metric = 'accuracy',tunegrid = xgb_grid_1)
我收到以下错误
Error in as.data.frame.default(data) :
cannot coerce class ""xgb.DMatrix"" to a data.frame
有关如何进行的任何建议吗?
答案 0 :(得分:1)
这是因为您在代码的最后部分输入了 dmodel
。尝试输入 objTrain_hashed
,这是一个矩阵,而不是 xgb.DMatrix
答案 1 :(得分:0)
sparse.model.matrix()而不是hashed.model.matrix怎么样... 它适用于我的电脑...... 并且不要转换为xgb.DMatrix() 把它放在train()函数中只是sparse.model.matrix()形式。
像...
model_data <- sparse.model.matrix(Y~., raw_data)
和
xgb_train1 <- train(Y ~.,model_data, <bla bla> ...)
希望它有效......谢谢。