gpt4 book ai didi

r - 无法将 xgb.DMatrix 传递给插入符

转载 作者:行者123 更新时间:2023-12-03 05:08:04 25 4
gpt4 key购买 nike

我正在尝试使用插入符号库调整 xgboost 的超参数来解决分类问题,由于我的数据集中有很多因素,并且 xgboost 喜欢将数据作为数字,所以我使用特征哈希创建了一个虚拟行,但是当我得到运行插入符列车,我收到错误

#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

关于我如何继续的任何建议?

最佳答案

这是因为您将 dmodel 输入到代码的最后部分。尝试输入 objTrain_hashed,它是一个矩阵,而不是 xgb.DMatrix

关于r - 无法将 xgb.DMatrix 传递给插入符,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40351054/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com