XGBOOST错误与多分类问题

发布于 2025-02-10 05:33:18 字数 1857 浏览 2 评论 0原文

在运行XGBoost多分类时,我会遇到错误,我将标签y设置为数字,但它不起作用。

我的代码:

xgb_train = xgb.DMatrix(data=data.matrix(trainX), label=trainY)
xgb_test = xgb.DMatrix(data=data.matrix(testX), label=testY)
watchlist = list(train=xgb_train, test=xgb_test)

xgmodel = xgb.train(data=xgb_train, 
                    objective = "multi:softmax",
                    num_class = 7,
                    max.depth=4, 
                    nrounds=6, 
                    eta = 0.3,
                    gamma = 0,
                    min_child_weight = 1,
                    verbose=1, 
                    watchlist=watchlist, 
                    eval_metric='mae')
Error in xgb.iter.update(bst$handle, dtrain, iteration - 1, obj) : 
  [13:48:41] amalgamation/../src/objective/multiclass_obj.cu:123: SoftmaxMultiClassObj: label must be in [0, num_class).
Stack trace:
  [bt] (0) 1   xgboost.so                          0x000000013a6a5ff4 dmlc::LogMessageFatal::~LogMessageFatal() + 116
  [bt] (1) 2   xgboost.so                          0x000000013a7d6efd xgboost::obj::SoftmaxMultiClassObj::GetGradient(xgboost::HostDeviceVector<float> const&, xgboost::MetaInfo const&, int, xgboost::HostDeviceVector<xgboost::detail::GradientPairInternal<float> >*) + 1069
  [bt] (2) 3   xgboost.so                          0x000000013a77c514 xgboost::LearnerImpl::UpdateOneIter(int, std::__1::shared_ptr<xgboost::DMatrix>) + 788
  [bt] (3) 4   xgboost.so                          0x000000013a73ff2c XGBoosterUpdateOneIter + 140
  [bt] (4) 5   xgboost.so                          0x000000013a6a28c3 XGBoosterUpdateOneIter_R + 67
  [bt] (5) 6   libR.dylib                          0x000000010e50da82 R_doDotCall + 1458
  [bt] (6) 7   libR.dyli

I got an error when running xgboost multi-classification, I set my label y as numeric, but it doesn't work.

My code:

xgb_train = xgb.DMatrix(data=data.matrix(trainX), label=trainY)
xgb_test = xgb.DMatrix(data=data.matrix(testX), label=testY)
watchlist = list(train=xgb_train, test=xgb_test)

xgmodel = xgb.train(data=xgb_train, 
                    objective = "multi:softmax",
                    num_class = 7,
                    max.depth=4, 
                    nrounds=6, 
                    eta = 0.3,
                    gamma = 0,
                    min_child_weight = 1,
                    verbose=1, 
                    watchlist=watchlist, 
                    eval_metric='mae')
Error in xgb.iter.update(bst$handle, dtrain, iteration - 1, obj) : 
  [13:48:41] amalgamation/../src/objective/multiclass_obj.cu:123: SoftmaxMultiClassObj: label must be in [0, num_class).
Stack trace:
  [bt] (0) 1   xgboost.so                          0x000000013a6a5ff4 dmlc::LogMessageFatal::~LogMessageFatal() + 116
  [bt] (1) 2   xgboost.so                          0x000000013a7d6efd xgboost::obj::SoftmaxMultiClassObj::GetGradient(xgboost::HostDeviceVector<float> const&, xgboost::MetaInfo const&, int, xgboost::HostDeviceVector<xgboost::detail::GradientPairInternal<float> >*) + 1069
  [bt] (2) 3   xgboost.so                          0x000000013a77c514 xgboost::LearnerImpl::UpdateOneIter(int, std::__1::shared_ptr<xgboost::DMatrix>) + 788
  [bt] (3) 4   xgboost.so                          0x000000013a73ff2c XGBoosterUpdateOneIter + 140
  [bt] (4) 5   xgboost.so                          0x000000013a6a28c3 XGBoosterUpdateOneIter_R + 67
  [bt] (5) 6   libR.dylib                          0x000000010e50da82 R_doDotCall + 1458
  [bt] (6) 7   libR.dyli

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

月牙弯弯 2025-02-17 05:33:18

好吧,我找到了答案。

我的标签包含从3到9开始的7个类。XgBoost接受从0开始的类标签,因此我减去所有列,模型成功运行。

Well, I found the answer.

My label contains 7 classes starting from 3 to 9. xgboost accepts class label starting from 0, so I subtract all column, and the model run successfully.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文