【问题标题】:Error while using predict() in XG-Boost in R在 R 的 XG-Boost 中使用 predict() 时出错
【发布时间】:2019-01-31 03:16:10
【问题描述】:

我一直在运行不同的算法来根据其他几个参数来预测 Facebook 帖子的效果。我尝试的最后一种方法是 XG-Boost。

即使在重新检查了我的代码和包的文档后,我仍然不断收到错误消息。我的训练数据和测试数据都已清理,所有因子都已转换为 1 和 0 的列。

// 处理测试和训练数据

temp.treat <- prepare(treatplan,temp, varRestriction = newvars)
test.treat <- prepare(treatplan,test, varRestriction = newvars) 

//训练模型

cv <- xgb.cv(data = as.matrix(temp.treat),
         label = temp$Reach,
         objective = "reg:linear",
         nrounds = 400, nfold = 5, eta = 0.3, depth = 6) 

//得到预测

test$pred <- predict(cv, as.matrix(test.treat))

数据训练没有抛出错误,但是在我运行预测命令的那一刻,我得到了错误 -

UseMethod("predict") 中的错误: 没有适用于“预测”的方法应用于“xgb.cv.synchronous”类的对象

谁能告诉我我做错了什么?

【问题讨论】:

标签: r prediction xgboost


【解决方案1】:

您应该使用该功能 xgboost() 在包中。

xgb.cv()只能帮你获取$evaluation表的相关信息

【讨论】:

  • 关于 ntrees.train rmse.train ntrees.test rmse.test
  • 考虑添加更多关于您的答案如何工作的解释 - 或许也可以将您的评论移至答案本身。
【解决方案2】:

正如 JMichaelJ 所建议的,这就是正在发生的事情:

您正在使用xgboost 包中的xgb.cv() 函数。 xgb.cv() 函数运行初步建模,以帮助您确定要指定的 nrounds(树)的适当数量等信息。这个可以在调用xgb.cv()后提取

cv <- xgb.cv(data = as.matrix(temp.treat),
         label = temp$Reach,
         objective = "reg:linear",
         nrounds = 400, nfold = 5, eta = 0.3, depth = 6) 

elog <- as.data.frame(cv$evaluation_log)

(nrounds <- which.min(elog$test_rmse_mean)) #this will print the number of trees you need

请务必记住,到目前为止,此步骤仅帮助您确定需要为模型指定的树数(nrounds)。现在您需要使用xgboost() 实际构建它:

nrounds <- #Whatever number it told you above

model <- xgboost(data = as.matrix(data.treat),
                 label = data$outcome,
                 nrounds = nrounds,       # this is where the xgb.cv() results matter
                 objective = "reg:linear",    #or whatever type you need
                 eta = 0.3,
                 depth = 6)

假设您已经为您的测试数据集构建了处理计划,您现在可以根据上面存储在model 中的输出使用predict()

test$predictions <- predict(model, as.matrix(test.treat))

您可能正在使用数据营课程中的示例代码。这就是我正在使用的。我有同样的问题。希望此解决方案也适用于您。

【讨论】:

    猜你喜欢
    • 2013-11-27
    • 2020-02-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-01-28
    • 1970-01-01
    • 2014-10-22
    • 2013-01-29
    相关资源
    最近更新 更多