Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

xgb.DMatrix Error: The length of labels must equal to the number of rows in the input data

Tags:

r

matrix

xgboost

I am using xgboost in R.

I created the xgb matrix fine using a matrix as input, but when I reduce the number in columns in the matrix data, I receive an error.

This works:

> dim(ctt1)

[1] 6401 5901

> xgbmat1 <- xgb.DMatrix(
     Matrix(data.matrix(ctt1)),
     label = as.matrix(as.numeric(data$V2)) - 1
  )

This does not:

> dim(ctt1[,nr])

[1] 6401 1048

xgbmat1 <- xgb.DMatrix(
    Matrix(data.matrix(ctt1[,nr])),
    label = as.matrix(as.numeric(data$V2)) - 1)

Error in xgb.setinfo(dmat, names(p), p[[1]]) : The length of labels must equal to the number of rows in the input data

like image 880
Ellen Xu Avatar asked Feb 08 '23 13:02

Ellen Xu


2 Answers

In my case I fixed this error by changing assign operation:

labels <- df_train$target_feature

like image 119
Andrii Avatar answered Feb 11 '23 14:02

Andrii


It turns out that by removing some columns, there are some rows with all 0s, and could not contribute to model.

like image 30
Ellen Xu Avatar answered Feb 11 '23 13:02

Ellen Xu