标签的长度必须等于输入数据中的行数

Gre*_*nXY 1 r xgboost

我不知道为什么我收到这个错误!我的数据training是稀疏矩阵.

dim(training)
> 14407 161

dim(label.train)
> 14407 1

xgb.train <- xgb.DMatrix(data = training, label = label.train)
> Error in setinfo.xgb.DMatrix(dmat, names(p), p[[1]]) : 
The length of labels must equal to the number of rows in the input data
Run Code Online (Sandbox Code Playgroud)

我检查了我的数据并且:

  • label.train 是一个data.frame
  • training 没有所有零行或列
  • 所有值training都是数字

PS.我的数据非常庞大,因此我无法发布可重现的代码,只需要提供可能出现此错误的人可能出错的提示.

Ada*_*uer 8

您收到错误是因为您的标签是data.frame.将它们作为向量或矩阵传递对我有用.

vec_y <- mtcars$vs
mat_y <- as.matrix(mtcars$vs)
df_y  <- mtcars[,8,drop=FALSE] #column vs is the 8th column

x <- as.matrix(mtcars[,-8])    #column vs is the 8th column

#vector labels: works
xgboost::xgb.DMatrix(data=x, label=vec_y)
#matrix labels: works
xgboost::xgb.DMatrix(data=x, label=mat_y)
#df labels: doesnt work
xgboost::xgb.DMatrix(data=x, label=df_y)
Run Code Online (Sandbox Code Playgroud)