在R中的XG-Boost中使用Forecate()时出错 [英] Error while using predict() in XG-Boost in R

查看:0
本文介绍了在R中的XG-Boost中使用Forecate()时出错的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在运行不同的算法,根据其他几个参数来预测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("Forecast")错误: 没有适用于"xgb.cv.Synchronous"类的对象的"Forecast"方法

有人能告诉我我做错了什么吗?

推荐答案

正如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
重要的是要记住,到目前为止,此步骤仅帮助您确定了需要为模型指定的树的数量。现在您需要使用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))

看起来您可能正在使用Data Camp课程中的示例代码。这就是我要用的。我也有同样的问题。希望此解决方案也适用于您。

这篇关于在R中的XG-Boost中使用Forecate()时出错的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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