【问题标题】:R: Missing data causes error with XGBoost / sparse.model.matrixR:缺少数据会导致 XGBoost / sparse.model.matrix 出错
【发布时间】:2018-02-15 11:19:16
【问题描述】:

据我所知,XGB 应该有处理丢失数据的好处,但是,每当我测试添加了一些 NA 的波士顿住房集时,我都会收到错误:

The length of labels must equal to the number of rows in the input data

我运行的代码是

trainm <- sparse.model.matrix(class ~ ., data = train)
train_label <- train[,"class"]
train_matrix <- xgb.DMatrix(data = as.matrix(trainm) label=train_label)

当我不添加 NA 时,一切运行正常。我很确定问题是从稀疏矩阵中删除了 NA,这会导致混淆,但我不知道如何解决。

我的代码是here

我们将非常感谢任何可以帮助我的反馈。

【问题讨论】:

  • 问题不在于NAtrain_label 中的观察数与trainm 中的观察数不同。如果您执行dim(trainm),您将获得369 x 15,而length(train_label) 将返回您403
  • 感谢您的评论。如果我注释掉“### Add a few NAs”部分(第 29-33 行),那么一切正常,所以除非我误解了某些内容,否则错误是由于缺少某些数据引起的?
  • 是的,如果您注释这些代码行,您将能够运行xgb.DMatrix

标签: r sparse-matrix xgboost


【解决方案1】:

在构建稀疏模型矩阵时,您需要采取纠正措施来处理 NA。休息一下,您的代码/数据没有问题。这是修改后的代码:

options(na.action='na.pass')
trainm <- sparse.model.matrix(class ~ ., data = train)
train_label <- train$class
train_matrix <- xgb.DMatrix(data = trainm, label=train$class)

【讨论】:

    猜你喜欢
    • 2020-10-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-05-10
    相关资源
    最近更新 更多