SKB
SKB

Reputation: 199

xgboost error : 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

sparse_matrix <- sparse.model.matrix(state ~ . -Month -LanID, data = smoted_data)
smoted_data_mat_label<-as.matrix(as.numeric(smoted_data$state))
smoted_data_mat_label = smoted_data[,"state"] == 1

    xgb.fit <- xgboost(
    data = sparse_matrix,
    label = smoted_data_mat_label,
    eta = 1.8,
    max_depth = 7,
    min_child_weight = 11, # could be anything all are showing same results
    nrounds = 9, # could be anything all are showing same results
    nfold = 10,
    objective = "binary:logistic",  # for regression models
    verbose = 1,               # silent,
    early_stopping_rounds = 10 # stop if no improvement for 10 consecutive trees
  )

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

length(smoted_data_mat_label)
[1] 22721
>   length(sparse_matrix)
[1] 41739681
  dim(smoted_data)
[1] 22721    31
   length(smoted_data$LanID)
[1] 22721

Earlier the code was working fine, now its causing below error not sure why? I have checked classes of sparse_matrix, it is matrix and label is also a matrix.

Upvotes: 0

Views: 1838

Answers (1)

SKB
SKB

Reputation: 199

This issue was occurring because in the new data some NA's has been introduced. Removed the NA's and algorithm worked fine.

Upvotes: 1

Related Questions