簡體   English   中英

R:缺少數據會導致XGBoost / sparse.model.matrix錯誤

[英]R: Missing data causes error with XGBoost / sparse.model.matrix

據我所知,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,但我不確定如何解決。

我的代碼在這里

任何對我有幫助的反饋將不勝感激。

在構建稀疏模型矩陣時,您需要采取糾正措施來處理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)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM