Error in XGBoost: Error in contrasts<-(tmp, value = contr.funs[1 + isOF[nn]]): 
  contrasts can be applied only to factors with 2 or more levels

param <- list(  objective           = "binary:logistic", 
                booster             = "gbtree",
                eval_metric         = "auc",
                eta                 = 0.01,
                max_depth           = 5,
                subsample           = 0.6,
                colsample_bytree    = 0.6

t1.y <- t1$target   

t1 <- sparse.model.matrix(target ~ ., data = t1)
#Error occurs after the above command. If i change the above statement to (replacing . by -1 to drop collinear columns)
t1 <- sparse.model.matrix(target ~ -1, data = t1)

No error occurs after changing to -1 but the dgcMatrix that is generated looks suspicious as it contains just one column and lot of rows. I was expecting the columns of the original matrix to be one-hot encoded rather than getting one columns as an output. If i proceed further ignoring the inspection of generated sparse matrix then I get an error after i run the xgboost below:

Error in xgb.iter.update(bst$handle, dtrain, iteration - 1, obj) : 
  [05:14:55] amalgamation/../src/objective/regression_obj.cc:108: label must be in [0,1] for logistic regression

dtrain <- xgb.DMatrix(data=t1, label=t1.y)
watchlist <- list(train=dtrain)
bst1 <- xgboost(   params              = param, 
                   data                = dtrain, 
                   nrounds             = 1750,
                   #watchlist           = watchlist,
                   verbose             = 1,
                   maximize            = FALSE

I tried to convert the target column to both factor and numeric (converted it to 0 & 1, since as.numeric converts it to 1 & 2). But it did not help :'(

When I convert the target to numeric then after running the Xgboost, I get the following error : Error in xgb.iter.update(bst$handle, dtrain, iteration - 1, obj) : [05:20:12] amalgamation/../src/tree/updater_colmaker.cc:162: Check failed: (n) > (0) colsample_bytree=0.6 is too small that no feature can be included

*t1 is a data.frame initially containing factor,numeric and integers which i am converting to sparse matrix

Thanks for the help!


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.