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

Viewed 10006

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

5 Answers

The proper way for creating the DBMatrix Like

    xgtrain <- xgb.DMatrix(data = as.matrix(X_train[,-5]), label = `X_train$item_cnt_month)`

drop the label column in data parameter and use same data set for create label column in index five i have item_cnt_month i drop it at run time and use same data set for referring label column

Before splitting your data, you need to turn it into a data frame. For Exemplo:

data <- read.csv(...)

data = as.data.frame(data)

Now you can set your train data and test data to use in your "sparse.model.matrix" and "xgb.DMatrix".

Related